@type32/codemirror-rich-obsidian-editor 0.1.23 → 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,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
|
|
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
|
-
${
|
|
79
|
+
${yamlResult.yaml}
|
|
77
80
|
---`;
|
|
78
81
|
if (hasFrontmatter) {
|
|
79
82
|
editorUtils.dispatch({
|
|
@@ -1,4 +1,50 @@
|
|
|
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
|
+
};
|
|
2
48
|
/**
|
|
3
49
|
* Parses a YAML/YML string into the specified type.
|
|
4
50
|
*
|
|
@@ -1,4 +1,20 @@
|
|
|
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
|
+
}
|
|
2
18
|
export function parseYaml(yamlString) {
|
|
3
19
|
if (!yamlString || yamlString.trim().length === 0) {
|
|
4
20
|
return { data: {} };
|
package/package.json
CHANGED