@type32/codemirror-rich-obsidian-editor 0.1.15 → 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
|
@@ -4,7 +4,9 @@ export declare function useEditorFrontmatter<T extends object = {}>(editor: Ref<
|
|
|
4
4
|
data?: T;
|
|
5
5
|
error?: Error;
|
|
6
6
|
};
|
|
7
|
+
updateFrontmatterProperties: (properties: Partial<T>) => void;
|
|
7
8
|
setFrontmatterProperties: (properties: Partial<T>) => void;
|
|
9
|
+
clearFrontmatter: () => void;
|
|
8
10
|
addFrontmatterProperty: (key: string, value: any) => void;
|
|
9
11
|
removeFrontmatterProperty: (key: string) => void;
|
|
10
12
|
};
|
|
@@ -10,26 +10,50 @@ export function useEditorFrontmatter(editor) {
|
|
|
10
10
|
}
|
|
11
11
|
return parseFrontmatter(doc);
|
|
12
12
|
}
|
|
13
|
-
function
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
89
|
+
updateFrontmatterProperties({ [key]: value });
|
|
51
90
|
}
|
|
52
91
|
function removeFrontmatterProperty(key) {
|
|
53
|
-
|
|
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