@type32/codemirror-rich-obsidian-editor 0.0.13 → 0.0.15
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
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type Ref } from 'vue';
|
|
2
|
+
import type { Frontmatter } from '../editor/types/editor-types.js';
|
|
3
|
+
export declare function useEditorFrontmatter<T extends object = {}>(editor: Ref<any>): {
|
|
4
|
+
getFrontmatter: () => {
|
|
5
|
+
data?: Frontmatter<T>;
|
|
6
|
+
error?: Error;
|
|
7
|
+
};
|
|
8
|
+
setFrontmatterProperties: (properties: Partial<Frontmatter<T>>) => void;
|
|
9
|
+
addFrontmatterProperty: (key: string, value: any) => void;
|
|
10
|
+
removeFrontmatterProperty: (key: string) => void;
|
|
11
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { load, dump } from "js-yaml";
|
|
2
|
+
import { useEditorUtils } from "./useEditorUtils.js";
|
|
3
|
+
export function useEditorFrontmatter(editor) {
|
|
4
|
+
const editorUtils = useEditorUtils(editor);
|
|
5
|
+
function getFrontmatter() {
|
|
6
|
+
const doc = editorUtils.getDoc();
|
|
7
|
+
if (!doc) {
|
|
8
|
+
return {};
|
|
9
|
+
}
|
|
10
|
+
const ast = editorUtils.parseMarkdownToAST(doc);
|
|
11
|
+
const firstNode = ast.topNode.firstChild;
|
|
12
|
+
if (!firstNode || firstNode.name !== "YAMLFrontMatter") {
|
|
13
|
+
return {};
|
|
14
|
+
}
|
|
15
|
+
const contentNode = firstNode.getChild("YAMLContent");
|
|
16
|
+
const yamlContent = contentNode ? doc.slice(contentNode.from, contentNode.to) : "";
|
|
17
|
+
try {
|
|
18
|
+
const data = load(yamlContent);
|
|
19
|
+
if (data === null || data === void 0) {
|
|
20
|
+
return { data: {} };
|
|
21
|
+
}
|
|
22
|
+
if (typeof data === "object") {
|
|
23
|
+
return { data };
|
|
24
|
+
}
|
|
25
|
+
return { error: new Error("Frontmatter is not a valid object.") };
|
|
26
|
+
} catch (e) {
|
|
27
|
+
return { error: e };
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function setFrontmatterProperties(properties) {
|
|
31
|
+
const doc = editorUtils.getDoc() || "";
|
|
32
|
+
const ast = editorUtils.parseMarkdownToAST(doc);
|
|
33
|
+
const firstNode = ast.topNode.firstChild;
|
|
34
|
+
let existingData = {};
|
|
35
|
+
let frontmatterNodeRange = { from: -1, to: -1 };
|
|
36
|
+
if (firstNode && firstNode.name === "YAMLFrontMatter") {
|
|
37
|
+
frontmatterNodeRange = { from: firstNode.from, to: firstNode.to };
|
|
38
|
+
const { data, error } = getFrontmatter();
|
|
39
|
+
if (data && !error) {
|
|
40
|
+
existingData = data;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const newData = { ...existingData, ...properties };
|
|
44
|
+
Object.keys(newData).forEach((key) => {
|
|
45
|
+
if (newData[key] === void 0) {
|
|
46
|
+
delete newData[key];
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
const newYamlContent = Object.keys(newData).length > 0 ? dump(newData, { skipInvalid: true }).trim() : "";
|
|
50
|
+
const newFrontmatterBlock = `---
|
|
51
|
+
${newYamlContent}
|
|
52
|
+
---`;
|
|
53
|
+
if (frontmatterNodeRange.from !== -1) {
|
|
54
|
+
editorUtils.dispatch({
|
|
55
|
+
changes: { from: frontmatterNodeRange.from, to: frontmatterNodeRange.to, insert: newFrontmatterBlock }
|
|
56
|
+
});
|
|
57
|
+
} else {
|
|
58
|
+
const insertText = doc.trim().length > 0 ? `${newFrontmatterBlock}
|
|
59
|
+
|
|
60
|
+
` : newFrontmatterBlock;
|
|
61
|
+
editorUtils.dispatch({
|
|
62
|
+
changes: { from: 0, to: 0, insert: insertText }
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function addFrontmatterProperty(key, value) {
|
|
67
|
+
setFrontmatterProperties({ [key]: value });
|
|
68
|
+
}
|
|
69
|
+
function removeFrontmatterProperty(key) {
|
|
70
|
+
setFrontmatterProperties({ [key]: void 0 });
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
getFrontmatter,
|
|
74
|
+
setFrontmatterProperties,
|
|
75
|
+
addFrontmatterProperty,
|
|
76
|
+
removeFrontmatterProperty
|
|
77
|
+
};
|
|
78
|
+
}
|
package/dist/runtime/editor/plugins/codemirror-plugin-proses/proseLinkCodemirrorViewPlugin.js
CHANGED
|
@@ -14,9 +14,12 @@ function buildLinkDecorations(state) {
|
|
|
14
14
|
const isActive = isNodeRangeActive(state, node.from, node.to) || cursorSelectionCoveredNode(poses.cursorFrom, poses.cursorTo, poses.nodeFrom, poses.nodeTo);
|
|
15
15
|
if (!isActive) {
|
|
16
16
|
const urlNode = node.getChild("URL");
|
|
17
|
+
const firstMark = node.getChildren("LinkMark")[0];
|
|
17
18
|
if (urlNode) {
|
|
18
19
|
const url = state.doc.sliceString(urlNode.from, urlNode.to);
|
|
19
|
-
|
|
20
|
+
let displayString = void 0;
|
|
21
|
+
if (firstMark?.to)
|
|
22
|
+
displayString = state.doc.sliceString(firstMark?.to, urlNode.from - 2);
|
|
20
23
|
decorations.push(Decoration.replace({
|
|
21
24
|
widget: new ProseVueComponentEmbedWidget(ImageEmbedComponent, { filePath: url, display: displayString }, node.from),
|
|
22
25
|
block: true
|