Skip to content

Commit

Permalink
Bug fixed: Incorrectly affects fields that are not managed by this pl…
Browse files Browse the repository at this point in the history
…ugin
  • Loading branch information
lijyze committed Jun 27, 2022
1 parent d0a8008 commit 560300b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-state-switcher",
"name": "Yaml Manager",
"version": "1.3.0",
"version": "1.3.1",
"minAppVersion": "0.12.0",
"description": "Keep you away from directly operating of yaml front matter",
"author": "Lijyze",
Expand Down
52 changes: 29 additions & 23 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,43 +47,49 @@ function getYaml(editor: Editor): string {

function generateActionKeyword(data: CommonUpdateParam | BulkUpdateParam) {
const {editor, action} = data;
const yaml = getYaml(editor);
const yamlSection = getYaml(editor);
const yaml = yamlSection.slice(4, -3);
const objectYaml = getObjectYaml(editor);
const objectSnippet: Record<string, unknown> = {};

const startPosition: EditorPosition = {line: 0, ch: 0};
const endPosition: EditorPosition = editor.offsetToPos(yaml.length);

if (!yaml) {
if (action === 'replace') objectYaml[data.key] = data.value;
if (action === 'insert') objectYaml[data.key] = [data.value];
if (action === 'bulk') {
Object.entries(data.updateDatas).forEach(([key, value]) => objectYaml[key] = value);
}
} else {
if (action === 'replace') objectYaml[data.key] = data.value;
if (action === 'insert') objectYaml[data.key] = objectYaml[data.key]? [...objectYaml[data.key], data.value]: [data.value];
if (action === 'remove' && objectYaml[data.key]) {
const newValue = objectYaml[data.key].filter((val: string) => val !== data.value);
newValue.length? objectYaml[data.key] = newValue: delete objectYaml[data.key];
}
if (action === 'bulk') {
data.removeDatas.forEach((key) => delete objectYaml[key])
Object.entries(data.updateDatas).forEach(([key, value]) => objectYaml[key] = value);
}
if (action === 'replace') objectSnippet[data.key] = data.value;

if (action === 'insert') objectSnippet[data.key] = objectYaml[data.key]? [...objectYaml[data.key], data.value]: [data.value];

if (action === 'remove') {
const newValue = objectYaml[data.key].filter((val: string) => val !== data.value);
objectSnippet[data.key] = newValue.length? newValue: null;
}

const replacement = `---\n${stringifyYaml(objectYaml)}---`;
if (action === 'bulk') {
Object.entries(data.updateDatas).forEach(([key, value]) => objectSnippet[key] = value );
data.removeDatas.forEach((key) => objectSnippet[key] = null)
}

const replacement = `---\n${generateReplacement(yaml, objectSnippet)}---`;
const startPosition: EditorPosition = {line: 0, ch: 0};
const endPosition: EditorPosition = editor.offsetToPos(yamlSection.length);

return {replacement, startPosition, endPosition}
}

function generateReplacement(yaml: string, snippet: Record<string, unknown>) {
return Object.entries(snippet).reduce((res, [key, value]) => {
const YAML_FIELD_REGEX = new RegExp(`(${key} *:).+?\\n(?=\\S|$)`, 'gs');

const replacement = (value === null)? '': stringifyYaml({[key]: value});

return yaml.match(YAML_FIELD_REGEX)? yaml.replace(YAML_FIELD_REGEX, replacement): `${yaml}${replacement}`;
}, yaml)
}

function flatYamlFields(yaml: string, flatFields: string[]): string {
const objectYaml = parseYaml(yaml.slice(4, -4));

return flatFields.reduce((res, key) => {
const YAML_FIELD_REGEX = new RegExp(`(${key}:).+?(?=\\n\\S|$)`, 'gs');

return yaml.replace(YAML_FIELD_REGEX, `$1 [${objectYaml[key].join(', ')}]`)
return yaml.match(YAML_FIELD_REGEX)? yaml.replace(YAML_FIELD_REGEX, `$1 [${objectYaml[key].join(', ')}]`): yaml;
}, yaml)
}

Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"1.1.2": "0.12.0",
"1.2.0": "0.12.0",
"1.2.1": "0.12.0",
"1.3.0": "0.12.0"
"1.3.0": "0.12.0",
"1.3.1": "0.12.0"
}

0 comments on commit 560300b

Please sign in to comment.