-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add outline_parse filter, clean up code
- Loading branch information
1 parent
0cf4e00
commit 28fd9ef
Showing
5 changed files
with
140 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export function findHeaders( | ||
root: any, | ||
selector: string, | ||
mode: "optin" | "dynamic", | ||
slugify: (text: string) => string, | ||
): { | ||
headers: Array<{ id: string; text: string; tag: string }>; | ||
markupChanged: boolean; | ||
} { | ||
const rawHeaders = [...root.querySelectorAll(selector)]; | ||
const headers = []; | ||
let markupChanged = false; | ||
for (const rawHeader of rawHeaders) { | ||
if (!rawHeader.getAttribute("id")) { | ||
if (mode != "dynamic") continue; | ||
markupChanged = true; | ||
} | ||
const text: string = rawHeader.rawText; | ||
const id: string = rawHeader.getAttribute("id") || slugify(text); | ||
const tag: string = rawHeader.tagName.toLowerCase(); | ||
headers.push({ id, text, tag }); | ||
} | ||
return { headers, markupChanged }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import fs from "node:fs/promises"; | ||
|
||
const templateMap = new Map<{ lang: string; source: string }, string>(); | ||
|
||
export async function renderTemplate( | ||
this: any, | ||
config: any, | ||
template: string | { lang: string; source: string }, | ||
tmpDir: string, | ||
data: { headers: Array<{ id: string; text: string; tag: string }> }, | ||
): Promise<string> { | ||
const renderFile = config.getShortcode("eleventyDocumentOutlineRender"); | ||
if (typeof template == "string") { | ||
return await renderFile.call(this, template, data); | ||
} | ||
if (!templateMap.has(template)) { | ||
await fs.mkdir(tmpDir, { recursive: true }); | ||
const { lang, source } = template; | ||
const uuid = crypto.randomUUID(); | ||
const filePath = `${tmpDir}/${uuid}.${lang}`; | ||
await fs.writeFile(filePath, source); | ||
templateMap.set(template, filePath); | ||
} | ||
const path = templateMap.get(template); | ||
return await renderFile.call(this, path, data); | ||
} |