Skip to content

Commit

Permalink
hbvariables now ignores any variables in the script helper
Browse files Browse the repository at this point in the history
  • Loading branch information
haouarihk committed Nov 6, 2023
1 parent 66d628d commit 633eb6b
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 81 deletions.
95 changes: 48 additions & 47 deletions src/context-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,53 +204,54 @@ export default class ContextManager {
return contexts;
}

extractVariablesFromTemplate(templateContent: string): string[] {
const ast: hbs.AST.Program =
Handlebars.parseWithoutProcessing(templateContent);

const extractVariablesFromBody = (
body: hbs.AST.Statement[],
eachContext: string | null = null
): string[] => {
return body
.flatMap((statement: hbs.AST.Statement) => {
if (statement.type === "MustacheStatement") {
const moustacheStatement: hbs.AST.MustacheStatement =
statement as hbs.AST.MustacheStatement;
const paramsExpressionList =
moustacheStatement.params as hbs.AST.PathExpression[];
const pathExpression =
moustacheStatement.path as hbs.AST.PathExpression;
const fullPath = eachContext
? `${eachContext}.${pathExpression.original}`
: pathExpression.original;

return paramsExpressionList[0]?.original || fullPath;
} else if (
statement.type === "BlockStatement" &&
// @ts-ignore
statement.path.original === "each"
) {
const blockStatement: hbs.AST.BlockStatement =
statement as hbs.AST.BlockStatement;
const eachVariable = blockStatement.path.original;
// @ts-ignore
const eachContext = blockStatement.params[0]?.original;

return extractVariablesFromBody(
blockStatement.program.body,
eachContext
);
} else {
return [];
}
})
.filter((value, index, self) => self.indexOf(value) === index);
};

const handlebarsVariables = extractVariablesFromBody(ast.body);
return handlebarsVariables;
}
// DEPRICATED
// extractVariablesFromTemplate(templateContent: string): string[] {
// const ast: hbs.AST.Program =
// Handlebars.parseWithoutProcessing(templateContent);

// const extractVariablesFromBody = (
// body: hbs.AST.Statement[],
// eachContext: string | null = null
// ): string[] => {
// return body
// .flatMap((statement: hbs.AST.Statement) => {
// if (statement.type === "MustacheStatement") {
// const moustacheStatement: hbs.AST.MustacheStatement =
// statement as hbs.AST.MustacheStatement;
// const paramsExpressionList =
// moustacheStatement.params as hbs.AST.PathExpression[];
// const pathExpression =
// moustacheStatement.path as hbs.AST.PathExpression;
// const fullPath = eachContext
// ? `${eachContext}.${pathExpression.original}`
// : pathExpression.original;

// return paramsExpressionList[0]?.original || fullPath;
// } else if (
// statement.type === "BlockStatement" &&
// // @ts-ignore
// statement.path.original === "each"
// ) {
// const blockStatement: hbs.AST.BlockStatement =
// statement as hbs.AST.BlockStatement;
// const eachVariable = blockStatement.path.original;
// // @ts-ignore
// const eachContext = blockStatement.params[0]?.original;

// return extractVariablesFromBody(
// blockStatement.program.body,
// eachContext
// );
// } else {
// return [];
// }
// })
// .filter((value, index, self) => self.indexOf(value) === index);
// };

// const handlebarsVariables = extractVariablesFromBody(ast.body);
// return handlebarsVariables;
// }

async getTemplateContext(props: {
editor?: Editor;
Expand Down
11 changes: 6 additions & 5 deletions src/text-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -741,15 +741,16 @@ ${removeYAML(content)}
this.contextManager.splitTemplate(templateContent);

const variables = this.contextManager.getHBVariablesOfTemplate(
preRunnerContent,
inputContent,
outputContent,
preRunnerContent
);

console.log(
{ variables },
this.contextManager.extractVariablesFromTemplate(inputContent)
);
console.log({
taking: "variables",
outputContent,
variables
})

const metadata = this.getMetadata(props.templatePath || "");
const tempateContext = await this.contextManager.getTemplateContext(props);
Expand Down
52 changes: 27 additions & 25 deletions src/ui/settings/sections/considered-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,36 @@ export default function ConsideredContextSetting(props: {
</SettingItem>
</SettingsSection>
<SettingsSection
title="Considered Context For Templates"
title="Template Settings"
className="flex w-full flex-col"
register={props.register}
id={sectionId}
>
<SettingItem
name="{{Context}} Template"
description="Template for {{context}} variable"
register={props.register}
sectionId={sectionId}
textArea
>
<textarea
placeholder="Textarea will autosize to fit the content"
className="resize-y"
value={
global.plugin.settings.context.contextTemplate ||
global.plugin.defaultSettings.context.contextTemplate
}
onChange={async (e) => {
global.plugin.settings.context.contextTemplate = e.target.value;
global.triggerReload();
await global.plugin.saveSettings();
}}
spellCheck={false}
rows={10}
/>
</SettingItem>
<AvailableVars vars={contextVariablesObj} />

{(["includeClipboard"] as (keyof Context)[])
// .filter((d) => !contextNotForTemplate.contains(d as any))
.map((key) => {
Expand Down Expand Up @@ -171,30 +196,7 @@ export default function ConsideredContextSetting(props: {
</SettingItem>
);
})}
<SettingItem
name="{{Context}} Template"
description="Template for {{context}} variable"
register={props.register}
sectionId={sectionId}
textArea
>
<textarea
placeholder="Textarea will autosize to fit the content"
className="resize-y"
value={
global.plugin.settings.context.contextTemplate ||
global.plugin.defaultSettings.context.contextTemplate
}
onChange={async (e) => {
global.plugin.settings.context.contextTemplate = e.target.value;
global.triggerReload();
await global.plugin.saveSettings();
}}
spellCheck={false}
rows={10}
/>
</SettingItem>
<AvailableVars vars={contextVariablesObj} />

<SettingItem
name="Allow scripts"
description="Only enable this if you trust the authors of the templates, or know what you're doing."
Expand Down
8 changes: 4 additions & 4 deletions src/ui/settings/sections/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export default function SectionsMain() {
!searchTerm.length
? Object.entries(items)
: Object.entries(items).filter(([key, val]) =>
`${val.term} ${items[val.sectionId]?.term}`.includes(searchTerm)
),
`${val.term} ${items[val.sectionId]?.term}`.includes(searchTerm)
),
[items, searchTerm]
);

Expand Down Expand Up @@ -92,11 +92,11 @@ export default function SectionsMain() {
</div>
<ProviderSetting register={register} />
<AdvancedSetting register={register} />
<OtherProvidersSetting register={register} />
<DMPSetting register={register} />
<AutoSuggestSetting register={register} />
<ConsideredContextSetting register={register} />
<ExtractorOptionsSetting register={register} />
<AutoSuggestSetting register={register} />
<OtherProvidersSetting register={register} />
<OptionsSetting register={register} />
</div>
);
Expand Down
12 changes: 12 additions & 0 deletions src/utils/barhandles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ const ignoredVariables = ["output", "this", "true", "false", "script"];
const defaultHelpers = ["if", "unless", "with", "each"];

export const getHBValues = (text: string) => {
console.log({
text,
edited: removeScriptOccurrences(text)
})
text = removeScriptOccurrences(text);

const re = /{{[{]?[{]?(.*?)[}]?[}]?}}/g;
const tags: string[] = [];
let matches: any;
Expand Down Expand Up @@ -157,4 +163,10 @@ function extractVariablesAndStrings(input: string): string[] {
}

return results;
}

function removeScriptOccurrences(text: string): string {
const pattern = /\{\{#script\}\}[\s\S]*?\{\{\/script\}\}/g;
const pattern2 = /\{\{\{\{script\}\}\}\}[\s\S]*?\{\{\{\{\/script\}\}\}\}/g;
return text.replace(pattern2, "").replace(pattern, "");
}

0 comments on commit 633eb6b

Please sign in to comment.