@type32/codemirror-rich-obsidian-editor 0.1.14 → 0.1.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@type32/codemirror-rich-obsidian-editor",
3
3
  "configKey": "cmOfmEditor",
4
- "version": "0.1.14",
4
+ "version": "0.1.16",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.2",
7
7
  "unbuild": "3.6.1"
@@ -1,11 +1,12 @@
1
1
  import { type Ref } from 'vue';
2
- import type { Frontmatter } from '../editor/types/editor-types.js';
3
2
  export declare function useEditorFrontmatter<T extends object = {}>(editor: Ref<any>): {
4
3
  getFrontmatter: () => {
5
- data?: Frontmatter<T>;
4
+ data?: T;
6
5
  error?: Error;
7
6
  };
8
- setFrontmatterProperties: (properties: Partial<Frontmatter<T>>) => void;
7
+ updateFrontmatterProperties: (properties: Partial<T>) => void;
8
+ setFrontmatterProperties: (properties: Partial<T>) => void;
9
+ clearFrontmatter: () => void;
9
10
  addFrontmatterProperty: (key: string, value: any) => void;
10
11
  removeFrontmatterProperty: (key: string) => void;
11
12
  };
@@ -10,26 +10,50 @@ export function useEditorFrontmatter(editor) {
10
10
  }
11
11
  return parseFrontmatter(doc);
12
12
  }
13
- function setFrontmatterProperties(properties) {
13
+ function updateFrontmatterProperties(properties) {
14
14
  const doc = editorUtils.getDoc() || "";
15
15
  const ast = editorUtils.parseMarkdownToAST(doc);
16
16
  const firstNode = ast.topNode.firstChild;
17
17
  let existingData = {};
18
- let frontmatterNodeRange = { from: -1, to: -1 };
19
- if (firstNode && firstNode.name === "YAMLFrontMatter") {
20
- frontmatterNodeRange = { from: firstNode.from, to: firstNode.to };
18
+ if (firstNode && (firstNode.name === "Frontmatter" || firstNode.name === "YAMLFrontMatter")) {
21
19
  const { data, error } = getFrontmatter();
22
20
  if (data && !error) {
23
21
  existingData = data;
24
22
  }
25
23
  }
26
24
  const newData = { ...existingData, ...properties };
25
+ setFrontmatterProperties(newData);
26
+ }
27
+ function setFrontmatterProperties(properties) {
28
+ const doc = editorUtils.getDoc() || "";
29
+ const ast = editorUtils.parseMarkdownToAST(doc);
30
+ const firstNode = ast.topNode.firstChild;
31
+ let frontmatterNodeRange = { from: -1, to: -1 };
32
+ if (firstNode && (firstNode.name === "Frontmatter" || firstNode.name === "YAMLFrontMatter")) {
33
+ frontmatterNodeRange = { from: firstNode.from, to: firstNode.to };
34
+ const { error } = getFrontmatter();
35
+ if (error) return;
36
+ }
37
+ const newData = { ...properties };
27
38
  Object.keys(newData).forEach((key) => {
28
39
  if (newData[key] === void 0) {
29
40
  delete newData[key];
30
41
  }
31
42
  });
32
- const newYamlContent = Object.keys(newData).length > 0 ? dump(newData, { skipInvalid: true }).trim() : "";
43
+ const hasContent = Object.keys(newData).length > 0;
44
+ if (!hasContent) {
45
+ if (frontmatterNodeRange.from !== -1) {
46
+ const endPos = frontmatterNodeRange.to;
47
+ let removeEnd = endPos;
48
+ if (doc[endPos] === "\n") removeEnd++;
49
+ if (doc[endPos + 1] === "\n") removeEnd++;
50
+ editorUtils.dispatch({
51
+ changes: { from: frontmatterNodeRange.from, to: removeEnd, insert: "" }
52
+ });
53
+ }
54
+ return;
55
+ }
56
+ const newYamlContent = dump(newData, { skipInvalid: true }).trim();
33
57
  const newFrontmatterBlock = `---
34
58
  ${newYamlContent}
35
59
  ---`;
@@ -40,21 +64,38 @@ ${newYamlContent}
40
64
  } else {
41
65
  const insertText = doc.trim().length > 0 ? `${newFrontmatterBlock}
42
66
 
43
- ` : newFrontmatterBlock;
67
+ ` : `${newFrontmatterBlock}
68
+ `;
44
69
  editorUtils.dispatch({
45
70
  changes: { from: 0, to: 0, insert: insertText }
46
71
  });
47
72
  }
48
73
  }
74
+ function clearFrontmatter() {
75
+ const doc = editorUtils.getDoc() || "";
76
+ const ast = editorUtils.parseMarkdownToAST(doc);
77
+ const firstNode = ast.topNode.firstChild;
78
+ if (firstNode && (firstNode.name === "Frontmatter" || firstNode.name === "YAMLFrontMatter")) {
79
+ const endPos = firstNode.to;
80
+ let removeEnd = endPos;
81
+ if (doc[endPos] === "\n") removeEnd++;
82
+ if (doc[endPos + 1] === "\n") removeEnd++;
83
+ editorUtils.dispatch({
84
+ changes: { from: firstNode.from, to: removeEnd, insert: "" }
85
+ });
86
+ }
87
+ }
49
88
  function addFrontmatterProperty(key, value) {
50
- setFrontmatterProperties({ [key]: value });
89
+ updateFrontmatterProperties({ [key]: value });
51
90
  }
52
91
  function removeFrontmatterProperty(key) {
53
- setFrontmatterProperties({ [key]: void 0 });
92
+ updateFrontmatterProperties({ [key]: void 0 });
54
93
  }
55
94
  return {
56
95
  getFrontmatter,
96
+ updateFrontmatterProperties,
57
97
  setFrontmatterProperties,
98
+ clearFrontmatter,
58
99
  addFrontmatterProperty,
59
100
  removeFrontmatterProperty
60
101
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@type32/codemirror-rich-obsidian-editor",
3
- "version": "0.1.14",
3
+ "version": "0.1.16",
4
4
  "description": "OFM Editor Component for Nuxt.",
5
5
  "repository": "https://github.com/Type-32/codemirror-rich-obsidian",
6
6
  "license": "MIT",