diff --git a/CHANGELOG.md b/CHANGELOG.md index ddf930f8..fba75d54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - [#570](https://github.com/estruyf/vscode-front-matter/issues/570): Clear empty values on content creation and editing - [#645](https://github.com/estruyf/vscode-front-matter/issues/645): French localization added (thanks to [Clément Barbaza](https://github.com/cba85)) +- [#649](https://github.com/estruyf/vscode-front-matter/issues/649): Parse optional variables from snippets ### 🎨 Enhancements diff --git a/src/helpers/SnippetParser.ts b/src/helpers/SnippetParser.ts index c2fb95f6..c84a980d 100644 --- a/src/helpers/SnippetParser.ts +++ b/src/helpers/SnippetParser.ts @@ -8,11 +8,23 @@ export class SnippetParser { closingTags: string = ']]' ): string[] { const template = SnippetParser.template(value); - return Mustache.parse(template, [openingTags, closingTags]) - .filter((v) => v[0] === 'name' || v[0] === '&') - .map((v) => { - return v[1]; - }); + const parseTree = Mustache.parse(template, [openingTags, closingTags]); + + const flattenVariablesFromParseTree = (acc: any, v: any) => { + if (v[0] === 'name') { + return acc.concat([v]); + } else if (v[0] === '&') { + return acc.concat([v]); + } else if (v[0] === '#') { + return acc.concat(v[4].reduce(flattenVariablesFromParseTree, [])); + } else { + return acc; + } + }; + + const variables = parseTree.reduce(flattenVariablesFromParseTree, []).map((v: any) => v[1]); + + return variables; } public static render(