Skip to content

Commit

Permalink
feat: more customizations in the generated template
Browse files Browse the repository at this point in the history
  • Loading branch information
danielo515 committed May 30, 2024
1 parent 8bf4fb4 commit 6db7d29
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
13 changes: 13 additions & 0 deletions src/views/components/TemplateBuilder.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
$: fields = model.fields;
$: code = model.code;
$: options = model.options;
</script>

<div class="modal-form flex flex-col gap-2">
Expand Down Expand Up @@ -56,6 +57,18 @@
</label>
</div>
</div>

<div class="flex flex-col flex-1">
<h3>Options</h3>
<label>
<input
type="checkbox"
checked={$options.includeFences}
on:change={(e) => ($options.includeFences = e.currentTarget.checked)}
/>
<span>Include frontmatter fences</span>
</label>
</div>
<div class="flex flex-col flex-1">
<h3>Template</h3>
<div class="flex gap-1">
Expand Down
11 changes: 6 additions & 5 deletions src/views/components/TemplateBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function compileOpenForm(formName: string, fieldsToOmit: string[], usesGlobal: b
}

function compileTemplaterTemplate(formName: string) {
return (fields: Field[]) => {
return ([fields, options]: [Field[], { includeFences: boolean }]) => {
const fieldsToInclude = fields.filter((field): field is FieldOption => !field.omit);
const fieldsToOmit = fields.filter((field): field is OmitedFieldOption => field.omit);
const openTheform = compileOpenForm(
Expand All @@ -70,12 +70,12 @@ function compileTemplaterTemplate(formName: string) {
console.log(openTheform);

return [
`<% "---" %>`,
options.includeFences ? `<% "---" %>` : "",
`<%*`,
` ${openTheform}`,
` ${compileFrontmatter(fieldsToInclude)}`,
`-%>`,
`<% "---" -%>`,
options.includeFences ? `<% "---" -%>` : "",
].join("\n");
};
}
Expand All @@ -84,8 +84,9 @@ export const makeModel = (formDefinition: FormDefinition) => {
const fields = writable(
formDefinition.fields.reduce((acc, { name }) => [...acc, Field(name)], [] as Field[]),
);
const options = writable({ includeFences: true });

const code = derived(fields, compileTemplaterTemplate(formDefinition.name));
const code = derived([fields, options], compileTemplaterTemplate(formDefinition.name));

function setField(name: string, newValues: Partial<Field>) {
console.log({ name, newValues });
Expand Down Expand Up @@ -114,7 +115,7 @@ export const makeModel = (formDefinition: FormDefinition) => {
function omitField(name: string, value: boolean) {
setField(name, { omit: value } as Field);
}
return { fields, setField, code, omitField, toggleAllFrontmatter };
return { fields, setField, code, omitField, toggleAllFrontmatter, options };
};

export type TemplateBuilderModel = ReturnType<typeof makeModel>;

0 comments on commit 6db7d29

Please sign in to comment.