summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/static_site_editor/services/parse_source_file.js
blob: d4fc8b2edb6669d7b06bc743e048cff62e22e6a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { frontMatterify, stringify } from './front_matterify';

const parseSourceFile = raw => {
  const remake = source => frontMatterify(source);

  let editable = remake(raw);

  const syncContent = (newVal, isBody) => {
    if (isBody) {
      editable.content = newVal;
    } else {
      editable = remake(newVal);
    }
  };

  const content = (isBody = false) => (isBody ? editable.content : stringify(editable));

  const matter = () => editable.matter;

  const syncMatter = settings => {
    editable.matter = settings;
  };

  const isModified = () => stringify(editable) !== raw;

  const hasMatter = () => Boolean(editable.matter);

  return {
    matter,
    syncMatter,
    content,
    syncContent,
    isModified,
    hasMatter,
  };
};

export default parseSourceFile;