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

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.22",
4
+ "version": "0.1.24",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.2",
7
7
  "unbuild": "3.6.1"
package/dist/module.mjs CHANGED
@@ -5,8 +5,11 @@ const module$1 = defineNuxtModule({
5
5
  name: "@type32/codemirror-rich-obsidian-editor",
6
6
  configKey: "cmOfmEditor"
7
7
  },
8
- // Default configuration options of the Nuxt module
9
- defaults: {},
8
+ moduleDependencies: {
9
+ "@nuxt/ui": {
10
+ version: ">=4.4.0"
11
+ }
12
+ },
10
13
  setup(_options, _nuxt) {
11
14
  const resolver = createResolver(import.meta.url);
12
15
  addComponentsDir({
@@ -1,6 +1,5 @@
1
- import { dump } from "js-yaml";
2
1
  import { useEditorUtils } from "./useEditorUtils.js";
3
- import { parseFrontmatter } from "../utils/frontmatter.js";
2
+ import { parseFrontmatter, stringifyYaml } from "../utils/frontmatter.js";
4
3
  export function useEditorFrontmatter(editor) {
5
4
  const editorUtils = useEditorUtils(editor);
6
5
  function getFrontmatter() {
@@ -71,9 +70,13 @@ export function useEditorFrontmatter(editor) {
71
70
  }
72
71
  return false;
73
72
  }
74
- const newYamlContent = dump(newData, { skipInvalid: true }).trim();
73
+ const yamlResult = stringifyYaml(newData);
74
+ if (yamlResult.error) {
75
+ console.error("Error converting data to YAML:", yamlResult.error);
76
+ return false;
77
+ }
75
78
  const newFrontmatterBlock = `---
76
- ${newYamlContent}
79
+ ${yamlResult.yaml}
77
80
  ---`;
78
81
  if (hasFrontmatter) {
79
82
  editorUtils.dispatch({
@@ -1,4 +1,74 @@
1
1
  import type { Frontmatter } from '../editor/types/editor-types.js';
2
+ /**
3
+ * Converts a JavaScript object/data into a YAML string suitable for writing to files.
4
+ *
5
+ * @param data - The data to convert to YAML
6
+ * @param options - Optional formatting options
7
+ * @returns YAML string or error object
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * interface Config {
12
+ * title: string
13
+ * count: number
14
+ * tags: string[]
15
+ * }
16
+ *
17
+ * const data: Config = {
18
+ * title: 'Hello',
19
+ * count: 42,
20
+ * tags: ['vue', 'nuxt']
21
+ * }
22
+ *
23
+ * const result = stringifyYaml(data)
24
+ * if (result.yaml) {
25
+ * console.log(result.yaml)
26
+ * // Output:
27
+ * // title: Hello
28
+ * // count: 42
29
+ * // tags:
30
+ * // - vue
31
+ * // - nuxt
32
+ * }
33
+ * ```
34
+ */
35
+ export declare function stringifyYaml(data: any, options?: {
36
+ /** Number of spaces for indentation (default: 2) */
37
+ indent?: number;
38
+ /** Skip invalid types instead of throwing (default: true) */
39
+ skipInvalid?: boolean;
40
+ /** Maximum line width (default: 80) */
41
+ lineWidth?: number;
42
+ /** Sort object keys (default: false) */
43
+ sortKeys?: boolean;
44
+ }): {
45
+ yaml?: string;
46
+ error?: Error;
47
+ };
48
+ /**
49
+ * Parses a YAML/YML string into the specified type.
50
+ *
51
+ * @param yamlString - The YAML string to parse
52
+ * @returns Parsed data or error object
53
+ *
54
+ * @example
55
+ * ```typescript
56
+ * interface Config {
57
+ * title: string
58
+ * count: number
59
+ * }
60
+ *
61
+ * const yaml = 'title: Hello\ncount: 42'
62
+ * const result = parseYaml<Config>(yaml)
63
+ * if (result.data) {
64
+ * console.log(result.data.title) // "Hello"
65
+ * }
66
+ * ```
67
+ */
68
+ export declare function parseYaml<T = any>(yamlString: string): {
69
+ data?: T;
70
+ error?: Error;
71
+ };
2
72
  /**
3
73
  * Lightning-fast frontmatter parser using string operations instead of AST parsing.
4
74
  * Optimized for performance - parses in microseconds instead of milliseconds.
@@ -1,4 +1,37 @@
1
- import { load } from "js-yaml";
1
+ import { load, dump } from "js-yaml";
2
+ export function stringifyYaml(data, options) {
3
+ if (data === null || data === void 0) {
4
+ return { yaml: "" };
5
+ }
6
+ try {
7
+ const yaml = dump(data, {
8
+ indent: options?.indent ?? 2,
9
+ skipInvalid: options?.skipInvalid ?? true,
10
+ lineWidth: options?.lineWidth ?? 80,
11
+ sortKeys: options?.sortKeys ?? false
12
+ });
13
+ return { yaml: yaml.trim() };
14
+ } catch (e) {
15
+ return { error: e };
16
+ }
17
+ }
18
+ export function parseYaml(yamlString) {
19
+ if (!yamlString || yamlString.trim().length === 0) {
20
+ return { data: {} };
21
+ }
22
+ try {
23
+ const data = load(yamlString);
24
+ if (data === null || data === void 0) {
25
+ return { data: {} };
26
+ }
27
+ if (typeof data === "object") {
28
+ return { data };
29
+ }
30
+ return { data };
31
+ } catch (e) {
32
+ return { error: e };
33
+ }
34
+ }
2
35
  export function parseFrontmatter(markdownText) {
3
36
  if (!markdownText) {
4
37
  return { error: new Error("No markdown text provided") };
@@ -20,16 +53,9 @@ export function parseFrontmatter(markdownText) {
20
53
  return { data: {} };
21
54
  }
22
55
  const yamlContent = markdownText.slice(yamlStart, closingFenceIndex);
23
- try {
24
- const data = load(yamlContent);
25
- if (data === null || data === void 0) {
26
- return { data: {} };
27
- }
28
- if (typeof data === "object") {
29
- return { data };
30
- }
56
+ const result = parseYaml(yamlContent);
57
+ if (result.data && typeof result.data !== "object") {
31
58
  return { error: new Error("Frontmatter is not a valid object.") };
32
- } catch (e) {
33
- return { error: e };
34
59
  }
60
+ return result;
35
61
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@type32/codemirror-rich-obsidian-editor",
3
- "version": "0.1.22",
3
+ "version": "0.1.24",
4
4
  "description": "OFM Editor Component for Nuxt.",
5
5
  "repository": "https://github.com/Type-32/codemirror-rich-obsidian",
6
6
  "license": "MIT",