mirror of
https://github.com/estruyf/vscode-front-matter.git
synced 2026-08-07 01:13:08 +02:00
#649 - Update snippet parser
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user