diff --git a/CHANGELOG.md b/CHANGELOG.md index c1e1908a..ff02375e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,7 @@ - [#520](https://github.com/estruyf/vscode-front-matter/issues/520): Add the URL protocol to the host on opening the preview if it's missing - [#521](https://github.com/estruyf/vscode-front-matter/issues/521): Fix empty snippets dashboard placeholder - [#526](https://github.com/estruyf/vscode-front-matter/issues/526): Fix card content menu +- [#529](https://github.com/estruyf/vscode-front-matter/issues/529): Fix YAML parsing in Windows which added an extra carriage return - [#531](https://github.com/estruyf/vscode-front-matter/issues/531): Fix prettier update which caused data views to not render list items - [#539](https://github.com/estruyf/vscode-front-matter/issues/539): Fix the override of the default file prefix on content creation diff --git a/src/parsers/ParserEngines.ts b/src/parsers/ParserEngines.ts index 161bd4f8..f6aede6e 100644 --- a/src/parsers/ParserEngines.ts +++ b/src/parsers/ParserEngines.ts @@ -13,6 +13,25 @@ export const getFormatOpts = (format: string): Format => { return formats[format]; }; +const removeCarriageReturn = (value: any) => { + const replacer = (crntValue: string) => { + return crntValue.replace(/\r/g, ''); + } + + if (typeof value === "string" && value.endsWith(`\r`)) { + value = replacer(value); + } else if (value instanceof Array) { + value = value.map(v => { + if (typeof v === "string" && v.endsWith(`\r`)) { + v = replacer(v); + } + return v; + }) + } + + return value; +} + export const Engines = { engines: { toml: { @@ -25,7 +44,7 @@ export const Engines = { }, yaml: { parse: (value: string) => { - return yaml.parse(value); + return yaml.parse(removeCarriageReturn(value)); }, stringify: (obj: any, options?: any) => { // Do our own parsing to keep the comments @@ -33,13 +52,18 @@ export const Engines = { const originalContent = FrontMatterParser.currentContent; FrontMatterParser.currentContent = null; - const frontMatter = getMatter(originalContent); + let frontMatter = getMatter(originalContent); if (frontMatter) { + if (typeof frontMatter === "string" && frontMatter.endsWith(`\r`)) { + frontMatter = frontMatter.substring(0, frontMatter.length - 1) + } + const docYaml = yaml.parseDocument(frontMatter); // Update all the new values for (const key in obj) { - docYaml.set(key, obj[key]); + let value = obj[key]; + docYaml.set(key, removeCarriageReturn(value)); } // Check if there are values to remove @@ -49,9 +73,11 @@ export const Engines = { } } - return docYaml.toString({ + let updatedValue = docYaml.toString({ lineWidth: 5000 }); + + return updatedValue; } }