Fixes #559
parent
b9bc3b1298
commit
7fec3fe587
|
@ -104,7 +104,7 @@ export async function extractFrontmatter(
|
||||||
// Updates the front matter of a markdown document and returns the text as a rendered string
|
// Updates the front matter of a markdown document and returns the text as a rendered string
|
||||||
export async function prepareFrontmatterDispatch(
|
export async function prepareFrontmatterDispatch(
|
||||||
tree: ParseTree,
|
tree: ParseTree,
|
||||||
data: Record<string, any>,
|
data: string | Record<string, any>,
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
let dispatchData: any = null;
|
let dispatchData: any = null;
|
||||||
await traverseTreeAsync(tree, async (t) => {
|
await traverseTreeAsync(tree, async (t) => {
|
||||||
|
@ -114,14 +114,20 @@ export async function prepareFrontmatterDispatch(
|
||||||
const yamlText = renderToText(bodyNode);
|
const yamlText = renderToText(bodyNode);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
let frontmatterText = "";
|
||||||
|
if (typeof data === "string") {
|
||||||
|
frontmatterText = yamlText + data + "\n";
|
||||||
|
} else {
|
||||||
const parsedYaml = await YAML.parse(yamlText) as any;
|
const parsedYaml = await YAML.parse(yamlText) as any;
|
||||||
const newData = { ...parsedYaml, ...data };
|
const newData = { ...parsedYaml, ...data };
|
||||||
|
frontmatterText = await YAML.stringify(newData);
|
||||||
|
}
|
||||||
// Patch inline
|
// Patch inline
|
||||||
dispatchData = {
|
dispatchData = {
|
||||||
changes: {
|
changes: {
|
||||||
from: bodyNode.from,
|
from: bodyNode.from,
|
||||||
to: bodyNode.to,
|
to: bodyNode.to,
|
||||||
insert: await YAML.stringify(newData),
|
insert: frontmatterText,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
|
@ -133,12 +139,19 @@ export async function prepareFrontmatterDispatch(
|
||||||
});
|
});
|
||||||
if (!dispatchData) {
|
if (!dispatchData) {
|
||||||
// If we didn't find frontmatter, let's add it
|
// If we didn't find frontmatter, let's add it
|
||||||
|
let frontmatterText = "";
|
||||||
|
if (typeof data === "string") {
|
||||||
|
frontmatterText = data + "\n";
|
||||||
|
} else {
|
||||||
|
frontmatterText = await YAML.stringify(data);
|
||||||
|
}
|
||||||
|
const fullFrontmatterText = "---\n" + frontmatterText +
|
||||||
|
"---\n";
|
||||||
dispatchData = {
|
dispatchData = {
|
||||||
changes: {
|
changes: {
|
||||||
from: 0,
|
from: 0,
|
||||||
to: 0,
|
to: 0,
|
||||||
insert: "---\n" + await YAML.stringify(data) +
|
insert: fullFrontmatterText,
|
||||||
"---\n",
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,7 +64,7 @@ export async function templateDirectiveRenderer(
|
||||||
|
|
||||||
// if it's a template injection (not a literal "include")
|
// if it's a template injection (not a literal "include")
|
||||||
if (directive === "use") {
|
if (directive === "use") {
|
||||||
newBody = await renderTemplate(newBody, pageMeta, parsedArgs);
|
newBody = (await renderTemplate(newBody, pageMeta, parsedArgs)).text;
|
||||||
|
|
||||||
// Recursively render directives
|
// Recursively render directives
|
||||||
const tree = await markdown.parseMarkdown(newBody);
|
const tree = await markdown.parseMarkdown(newBody);
|
||||||
|
|
|
@ -16,7 +16,7 @@ export async function renderTemplate(
|
||||||
templateText: string,
|
templateText: string,
|
||||||
pageMeta: PageMeta,
|
pageMeta: PageMeta,
|
||||||
data: any = {},
|
data: any = {},
|
||||||
): Promise<string> {
|
): Promise<{ frontmatter?: string; text: string }> {
|
||||||
const tree = await markdown.parseMarkdown(templateText);
|
const tree = await markdown.parseMarkdown(templateText);
|
||||||
const frontmatter: Partial<TemplateObject> = await extractFrontmatter(tree, {
|
const frontmatter: Partial<TemplateObject> = await extractFrontmatter(tree, {
|
||||||
removeFrontmatterSection: true,
|
removeFrontmatterSection: true,
|
||||||
|
@ -24,15 +24,20 @@ export async function renderTemplate(
|
||||||
});
|
});
|
||||||
templateText = renderToText(tree).trimStart();
|
templateText = renderToText(tree).trimStart();
|
||||||
// If a 'frontmatter' key was specified in the frontmatter, use that as the frontmatter
|
// If a 'frontmatter' key was specified in the frontmatter, use that as the frontmatter
|
||||||
|
let frontmatterText: string | undefined;
|
||||||
if (frontmatter.frontmatter) {
|
if (frontmatter.frontmatter) {
|
||||||
if (typeof frontmatter.frontmatter === "string") {
|
if (typeof frontmatter.frontmatter === "string") {
|
||||||
templateText = "---\n" + frontmatter.frontmatter + "---\n" + templateText;
|
frontmatterText = frontmatter.frontmatter;
|
||||||
} else {
|
} else {
|
||||||
templateText = "---\n" + (await YAML.stringify(frontmatter.frontmatter)) +
|
frontmatterText = await YAML.stringify(frontmatter.frontmatter);
|
||||||
"---\n" + templateText;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return handlebars.renderTemplate(templateText, data, { page: pageMeta });
|
return {
|
||||||
|
frontmatter: frontmatterText,
|
||||||
|
text: await handlebars.renderTemplate(templateText, data, {
|
||||||
|
page: pageMeta,
|
||||||
|
}),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -5,7 +5,7 @@ export function renderTemplate(
|
||||||
templateText: string,
|
templateText: string,
|
||||||
pageMeta: PageMeta,
|
pageMeta: PageMeta,
|
||||||
data: any = {},
|
data: any = {},
|
||||||
): Promise<string> {
|
): Promise<{ frontmatter?: string; text: string }> {
|
||||||
return system.invokeFunction(
|
return system.invokeFunction(
|
||||||
"template.renderTemplate",
|
"template.renderTemplate",
|
||||||
templateText,
|
templateText,
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
import { editor, handlebars, markdown, space, YAML } from "$sb/syscalls.ts";
|
import { editor, handlebars, markdown, space, YAML } from "$sb/syscalls.ts";
|
||||||
import { extractFrontmatter } from "$sb/lib/frontmatter.ts";
|
import {
|
||||||
|
extractFrontmatter,
|
||||||
|
prepareFrontmatterDispatch,
|
||||||
|
} from "$sb/lib/frontmatter.ts";
|
||||||
import { renderToText } from "$sb/lib/tree.ts";
|
import { renderToText } from "$sb/lib/tree.ts";
|
||||||
import { niceDate, niceTime } from "$sb/lib/dates.ts";
|
import { niceDate, niceTime } from "$sb/lib/dates.ts";
|
||||||
import { readSettings } from "$sb/lib/settings_page.ts";
|
import { readSettings } from "$sb/lib/settings_page.ts";
|
||||||
|
@ -29,13 +32,26 @@ export async function templateSlashComplete(
|
||||||
export async function insertSlashTemplate(slashCompletion: SlashCompletion) {
|
export async function insertSlashTemplate(slashCompletion: SlashCompletion) {
|
||||||
const pageObject = await loadPageObject(slashCompletion.pageName);
|
const pageObject = await loadPageObject(slashCompletion.pageName);
|
||||||
|
|
||||||
let templateText = await space.readPage(slashCompletion.templatePage);
|
const templateText = await space.readPage(slashCompletion.templatePage);
|
||||||
templateText = await renderTemplate(templateText, pageObject);
|
let { frontmatter, text } = await renderTemplate(templateText, pageObject);
|
||||||
|
|
||||||
const cursorPos = await editor.getCursor();
|
const cursorPos = await editor.getCursor();
|
||||||
const carretPos = templateText.indexOf("|^|");
|
|
||||||
templateText = templateText.replace("|^|", "");
|
if (frontmatter) {
|
||||||
await editor.insertAtCursor(templateText);
|
frontmatter = frontmatter.trim();
|
||||||
|
const pageText = await editor.getText();
|
||||||
|
const tree = await markdown.parseMarkdown(pageText);
|
||||||
|
|
||||||
|
const dispatch = await prepareFrontmatterDispatch(tree, frontmatter);
|
||||||
|
if (cursorPos === 0) {
|
||||||
|
dispatch.selection = { anchor: frontmatter.length + 9 };
|
||||||
|
}
|
||||||
|
await editor.dispatch(dispatch);
|
||||||
|
}
|
||||||
|
|
||||||
|
const carretPos = text.indexOf("|^|");
|
||||||
|
text = text.replace("|^|", "");
|
||||||
|
await editor.insertAtCursor(text);
|
||||||
if (carretPos !== -1) {
|
if (carretPos !== -1) {
|
||||||
await editor.moveCursor(cursorPos + carretPos);
|
await editor.moveCursor(cursorPos + carretPos);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue