@type32/codemirror-rich-obsidian-editor 0.1.21 → 0.1.22

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.21",
4
+ "version": "0.1.22",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.2",
7
7
  "unbuild": "3.6.1"
@@ -7,6 +7,6 @@ export declare function useEditorFrontmatter<T extends object = {}>(editor: Ref<
7
7
  updateFrontmatterProperties: (properties: Partial<T>) => boolean;
8
8
  setFrontmatterProperties: (properties: Partial<T>) => boolean;
9
9
  clearFrontmatter: () => boolean;
10
- addFrontmatterProperty: (key: string, value: any) => void;
11
- removeFrontmatterProperty: (key: string) => void;
10
+ addFrontmatterProperty: (key: string, value: any) => boolean;
11
+ removeFrontmatterProperty: (key: string) => boolean;
12
12
  };
@@ -17,11 +17,10 @@ export function useEditorFrontmatter(editor) {
17
17
  }
18
18
  function updateFrontmatterProperties(properties) {
19
19
  try {
20
- const doc = editorUtils.getDoc() || "";
21
- const ast = editorUtils.parseMarkdownToAST(doc);
22
- const firstNode = ast.topNode.firstChild;
20
+ const doc = editorUtils.getDoc();
21
+ if (!doc) return false;
23
22
  let existingData = {};
24
- if (firstNode && (firstNode.name === "Frontmatter" || firstNode.name === "YAMLFrontMatter")) {
23
+ if (doc.startsWith("---\n") || doc.startsWith("---\r\n")) {
25
24
  const { data, error } = getFrontmatter();
26
25
  if (data && !error) {
27
26
  existingData = data;
@@ -30,21 +29,14 @@ export function useEditorFrontmatter(editor) {
30
29
  const newData = { ...existingData, ...properties };
31
30
  return setFrontmatterProperties(newData);
32
31
  } catch (e) {
33
- console.log(e);
32
+ console.error("Error updating frontmatter:", e);
34
33
  return false;
35
34
  }
36
35
  }
37
36
  function setFrontmatterProperties(properties) {
38
37
  try {
39
- const doc = editorUtils.getDoc() || "";
40
- const ast = editorUtils.parseMarkdownToAST(doc);
41
- const firstNode = ast.topNode.firstChild;
42
- let frontmatterNodeRange = { from: -1, to: -1 };
43
- if (firstNode && (firstNode.name === "Frontmatter" || firstNode.name === "YAMLFrontMatter")) {
44
- frontmatterNodeRange = { from: firstNode.from, to: firstNode.to };
45
- const { error } = getFrontmatter();
46
- if (error) return false;
47
- }
38
+ const doc = editorUtils.getDoc();
39
+ if (doc === void 0) return false;
48
40
  const newData = { ...properties };
49
41
  Object.keys(newData).forEach((key) => {
50
42
  if (newData[key] === void 0) {
@@ -52,15 +44,30 @@ export function useEditorFrontmatter(editor) {
52
44
  }
53
45
  });
54
46
  const hasContent = Object.keys(newData).length > 0;
47
+ let frontmatterStart = -1;
48
+ let frontmatterEnd = -1;
49
+ if (doc.startsWith("---\n") || doc.startsWith("---\r\n")) {
50
+ frontmatterStart = 0;
51
+ const yamlStart = doc.indexOf("\n", 3) + 1;
52
+ const closingFenceIndex = doc.indexOf("\n---", yamlStart);
53
+ if (closingFenceIndex !== -1) {
54
+ const afterFence = closingFenceIndex + 4;
55
+ const nextChar = doc[afterFence];
56
+ if (nextChar === void 0 || nextChar === "\n" || nextChar === "\r") {
57
+ frontmatterEnd = afterFence;
58
+ }
59
+ }
60
+ }
61
+ const hasFrontmatter = frontmatterStart !== -1 && frontmatterEnd !== -1;
55
62
  if (!hasContent) {
56
- if (frontmatterNodeRange.from !== -1) {
57
- const endPos = frontmatterNodeRange.to;
58
- let removeEnd = endPos;
59
- if (doc[endPos] === "\n") removeEnd++;
60
- if (doc[endPos + 1] === "\n") removeEnd++;
63
+ if (hasFrontmatter) {
64
+ let removeEnd = frontmatterEnd;
65
+ if (doc[removeEnd] === "\n" || doc[removeEnd] === "\r") removeEnd++;
66
+ if (doc[removeEnd] === "\n" || doc[removeEnd] === "\r") removeEnd++;
61
67
  editorUtils.dispatch({
62
- changes: { from: frontmatterNodeRange.from, to: removeEnd, insert: "" }
68
+ changes: { from: frontmatterStart, to: removeEnd, insert: "" }
63
69
  });
70
+ return true;
64
71
  }
65
72
  return false;
66
73
  }
@@ -68,11 +75,11 @@ export function useEditorFrontmatter(editor) {
68
75
  const newFrontmatterBlock = `---
69
76
  ${newYamlContent}
70
77
  ---`;
71
- if (frontmatterNodeRange.from !== -1) {
78
+ if (hasFrontmatter) {
72
79
  editorUtils.dispatch({
73
80
  changes: {
74
- from: frontmatterNodeRange.from,
75
- to: frontmatterNodeRange.to,
81
+ from: frontmatterStart,
82
+ to: frontmatterEnd,
76
83
  insert: newFrontmatterBlock
77
84
  }
78
85
  });
@@ -87,35 +94,43 @@ ${newYamlContent}
87
94
  }
88
95
  return true;
89
96
  } catch (e) {
90
- console.log(e);
97
+ console.error("Error setting frontmatter:", e);
91
98
  return false;
92
99
  }
93
100
  }
94
101
  function clearFrontmatter() {
95
102
  try {
96
- const doc = editorUtils.getDoc() || "";
97
- const ast = editorUtils.parseMarkdownToAST(doc);
98
- const firstNode = ast.topNode.firstChild;
99
- if (firstNode && (firstNode.name === "Frontmatter" || firstNode.name === "YAMLFrontMatter")) {
100
- const endPos = firstNode.to;
101
- let removeEnd = endPos;
102
- if (doc[endPos] === "\n") removeEnd++;
103
- if (doc[endPos + 1] === "\n") removeEnd++;
104
- editorUtils.dispatch({
105
- changes: { from: firstNode.from, to: removeEnd, insert: "" }
106
- });
103
+ const doc = editorUtils.getDoc();
104
+ if (!doc) return false;
105
+ if (!doc.startsWith("---\n") && !doc.startsWith("---\r\n")) {
106
+ return true;
107
107
  }
108
+ const yamlStart = doc.indexOf("\n", 3) + 1;
109
+ if (yamlStart === 0) return true;
110
+ const closingFenceIndex = doc.indexOf("\n---", yamlStart);
111
+ if (closingFenceIndex === -1) return true;
112
+ const afterFence = closingFenceIndex + 4;
113
+ const nextChar = doc[afterFence];
114
+ if (nextChar !== void 0 && nextChar !== "\n" && nextChar !== "\r") {
115
+ return true;
116
+ }
117
+ let removeEnd = afterFence;
118
+ if (doc[removeEnd] === "\n" || doc[removeEnd] === "\r") removeEnd++;
119
+ if (doc[removeEnd] === "\n" || doc[removeEnd] === "\r") removeEnd++;
120
+ editorUtils.dispatch({
121
+ changes: { from: 0, to: removeEnd, insert: "" }
122
+ });
108
123
  return true;
109
124
  } catch (e) {
110
- console.log(e);
125
+ console.error("Error clearing frontmatter:", e);
111
126
  return false;
112
127
  }
113
128
  }
114
129
  function addFrontmatterProperty(key, value) {
115
- updateFrontmatterProperties({ [key]: value });
130
+ return updateFrontmatterProperties({ [key]: value });
116
131
  }
117
132
  function removeFrontmatterProperty(key) {
118
- updateFrontmatterProperties({ [key]: void 0 });
133
+ return updateFrontmatterProperties({ [key]: void 0 });
119
134
  }
120
135
  return {
121
136
  getFrontmatter,
@@ -1,4 +1,8 @@
1
1
  import type { Frontmatter } from '../editor/types/editor-types.js';
2
+ /**
3
+ * Lightning-fast frontmatter parser using string operations instead of AST parsing.
4
+ * Optimized for performance - parses in microseconds instead of milliseconds.
5
+ */
2
6
  export declare function parseFrontmatter(markdownText: string): {
3
7
  data?: Frontmatter;
4
8
  error?: Error;
@@ -1,16 +1,25 @@
1
1
  import { load } from "js-yaml";
2
- import { parseMarkdownToAST } from "./markdownParser.js";
3
2
  export function parseFrontmatter(markdownText) {
4
3
  if (!markdownText) {
5
4
  return { error: new Error("No markdown text provided") };
6
5
  }
7
- const tree = parseMarkdownToAST(markdownText);
8
- const firstNode = tree.topNode.firstChild;
9
- if (!firstNode || firstNode.name !== "YAMLFrontMatter") {
6
+ if (!markdownText.startsWith("---\n") && !markdownText.startsWith("---\r\n")) {
10
7
  return { data: {} };
11
8
  }
12
- const contentNode = firstNode.getChild("YAMLContent");
13
- const yamlContent = contentNode ? markdownText.slice(contentNode.from, contentNode.to) : "";
9
+ const yamlStart = markdownText.indexOf("\n", 3) + 1;
10
+ if (yamlStart === 0) {
11
+ return { data: {} };
12
+ }
13
+ const closingFenceIndex = markdownText.indexOf("\n---", yamlStart);
14
+ if (closingFenceIndex === -1) {
15
+ return { data: {} };
16
+ }
17
+ const afterFence = closingFenceIndex + 4;
18
+ const nextChar = markdownText[afterFence];
19
+ if (nextChar !== void 0 && nextChar !== "\n" && nextChar !== "\r") {
20
+ return { data: {} };
21
+ }
22
+ const yamlContent = markdownText.slice(yamlStart, closingFenceIndex);
14
23
  try {
15
24
  const data = load(yamlContent);
16
25
  if (data === null || data === void 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@type32/codemirror-rich-obsidian-editor",
3
- "version": "0.1.21",
3
+ "version": "0.1.22",
4
4
  "description": "OFM Editor Component for Nuxt.",
5
5
  "repository": "https://github.com/Type-32/codemirror-rich-obsidian",
6
6
  "license": "MIT",