#649 - Update snippet parser

This commit is contained in:
Elio Struyf
2023-09-06 13:42:33 +02:00
parent 3469771a02
commit 7b6be79bb0
2 changed files with 18 additions and 5 deletions
+1
View File
@@ -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
+17 -5
View File
@@ -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(