Skip to content

Commit

Permalink
Refactor yaml parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mnaoumov committed Oct 1, 2024
1 parent 0c203f2 commit f663d6a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 43 deletions.
8 changes: 0 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"@types/doctrine": "^0.0.9",
"@types/eslint": "^9.6.1",
"@types/eslint__js": "^8.42.3",
"@types/js-yaml": "^4.0.9",
"@types/luxon": "^3.4.2",
"@types/node": "^22.5.4",
"@types/parsimmon": "^1.10.9",
Expand All @@ -63,7 +62,6 @@
"eslint-plugin-verify-tsdoc": "^1.0.18",
"eventemitter3": "^5.0.1",
"glob": "^11.0.0",
"js-yaml": "^4.1.0",
"localforage": "^1.10.0",
"lru-cache": "^11.0.1",
"npm-run-all": "^4.1.5",
Expand Down
52 changes: 19 additions & 33 deletions src/obsidian/FrontMatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
*/

import {
DEFAULT_SCHEMA,
dump,
load,
Type
} from 'js-yaml';
import { App } from 'obsidian';
App,
parseYaml,
stringifyYaml
} from 'obsidian';

import type { MaybePromise } from '../Async.ts';
import { throwExpression } from '../Error.ts';
import { deepEqual } from '../Object.ts';
import type { PathOrFile } from './FileSystem.ts';
import { getFile } from './FileSystem.ts';
import { processWithRetry } from './Vault.ts';
Expand Down Expand Up @@ -76,17 +74,6 @@ export interface ObsidianPublishFrontMatter {
*/
export type CombinedFrontMatter<CustomFrontMatter> = CustomFrontMatter & ObsidianFrontMatter & Record<string, unknown>;

const TIMESTAMP_TYPE = new Type('tag:yaml.org,2002:timestamp', {
kind: 'scalar',
resolve: (data: unknown): boolean => data != null,
construct: (data: unknown): string => String(data),
represent: (data: object): unknown => data
});

const NO_TIMESTAMPS_YAML_SCHEMA = DEFAULT_SCHEMA.extend({
explicit: [TIMESTAMP_TYPE]
});

const FRONT_MATTER_REG_EXP = /^---\r?\n((?:.|\r?\n)*?)\r?\n?---(?:\r?\n|$)((?:.|\r?\n)*)/;

/**
Expand All @@ -106,34 +93,33 @@ export async function processFrontMatter<CustomFrontMatter = unknown>(app: App,
let frontMatterStr: string;
let mainContent: string;
if (match) {
frontMatterStr = match[1] ?? throwExpression(new Error('Front matter match is null'));
mainContent = match[2] ?? throwExpression(new Error('Front matter match is null'));
frontMatterStr = match[1]?.trim() ?? '';
mainContent = match[2]?.trim() ?? '';
} else {
frontMatterStr = '';
mainContent = content;
mainContent = content.trim();
}

if (!mainContent) {
mainContent = '\n';
} else {
mainContent = '\n' + mainContent.trim() + '\n';
mainContent = `\n${mainContent}\n`;
}

const oldFrontMatter = (parseYaml(frontMatterStr) ?? {}) as CombinedFrontMatter<CustomFrontMatter>;
const newFrontMatter = (parseYaml(frontMatterStr) ?? {}) as CombinedFrontMatter<CustomFrontMatter>;
await frontMatterFn(newFrontMatter);

if (deepEqual(oldFrontMatter, newFrontMatter)) {
return content;
}

const frontMatter = (load(frontMatterStr, { schema: NO_TIMESTAMPS_YAML_SCHEMA }) ?? {}) as CombinedFrontMatter<CustomFrontMatter>;
await frontMatterFn(frontMatter);
let newFrontMatterStr = dump(frontMatter, {
lineWidth: -1,
quotingType: '"',
schema: NO_TIMESTAMPS_YAML_SCHEMA
});
let newFrontMatterStr = stringifyYaml(newFrontMatter);
if (newFrontMatterStr === '{}\n') {
newFrontMatterStr = '';
}

const newContent = `---
${newFrontMatterStr}---
${mainContent}`;

const newContent = `---\n${newFrontMatterStr}---\n${mainContent}`;
return newContent;
});
}
Expand Down

0 comments on commit f663d6a

Please sign in to comment.