From bfbdb101930f52b381ef4e409c19d42488aeaf6d Mon Sep 17 00:00:00 2001 From: vrugtehagel Date: Fri, 19 Jul 2024 19:00:53 +0200 Subject: [PATCH] Remove option and fix renderFile errors on serve --- deno.json | 2 +- src/index.ts | 37 +++++++------------------------------ src/options.ts | 7 ------- src/render-template.ts | 25 +++++-------------------- 4 files changed, 13 insertions(+), 58 deletions(-) diff --git a/deno.json b/deno.json index 22f24e4..d8fb15a 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,6 @@ { "name": "@vrugtehagel/eleventy-document-outline", - "version": "0.1.4", + "version": "0.1.5", "exports": "./mod.ts", "publish": { "include": ["src/**", "mod.ts", "README.md"] diff --git a/src/index.ts b/src/index.ts index 535543a..c2cf297 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,3 @@ -import fs from "node:fs/promises"; -import { RenderPlugin } from "npm:@11ty/eleventy@^3.0.0-alpha.15"; import * as HTMLParser from "npm:node-html-parser@^6.1"; import type { EleventyDocumentOutlineOptions } from "./options.ts"; import { findHeaders } from "./find-headers.ts"; @@ -8,17 +6,6 @@ import { renderTemplate } from "./render-template.ts"; /** The Eleventy config object, should be a better type than "any" but alas */ type EleventyConfig = any; -/** We wrap the RenderPlugin with our own function, so Eleventy sees it as a - * different plugin. We also rename the shortcodes so that users are not - * bothered by the addition of the plugin under the hood. */ -function RenderPluginForEleventyDocumentOutline(config: EleventyConfig) { - return RenderPlugin(config, { - tagName: null, - tagNameFile: "eleventyDocumentOutlineRender", - accessGlobalData: true, - }); -} - /** The main plugin. Add this with `eleventyConfig.addPlugin(…);` Options are * all optional - see the `EleventyDocumentOutlineOptions` type for more * information. */ @@ -26,9 +13,7 @@ export function EleventyDocumentOutline( config: EleventyConfig, options: EleventyDocumentOutlineOptions = {}, ) { - /** We need this for the shortcode. If it already exists, then adding it - * again is fine and does nothing. */ - config.addPlugin(RenderPluginForEleventyDocumentOutline); + config.versionCheck(">=3.0.0-alpha.15"); const { headers: defaultSelector = "h1,h2,h3", @@ -44,7 +29,6 @@ export function EleventyDocumentOutline( }, mode: defaultMode = "optin", slugify = config.getFilter("slugify"), - tmpDir = "tmpDirEleventyDocumentOutline", } = options; const memory = new Map { const root = HTMLParser.parse(content); const { headers } = findHeaders(root, selector, "optin", slugify); - const data = { headers }; - return await renderTemplate.call(this, config, template, tmpDir, data); + const { page, eleventy } = this; + const data = { headers, page, eleventy }; + return await renderTemplate(template, data); }); /** If we have shortcodes, then we process HTML files, find UUIDs inside them @@ -135,10 +120,10 @@ export function EleventyDocumentOutline( headers, markupChanged, } = findHeaders(root, selector, mode, slugify); - const data = { headers }; + const { page, eleventy } = this; + const data = { headers, page, eleventy }; alteredParsedHTML ||= markupChanged; - const rendered = await renderTemplate - .call(this, config, template, tmpDir, data); + const rendered = await renderTemplate(template, data); replacements.set(uuid, rendered); })); let result = alteredParsedHTML ? root.toString() : content; @@ -147,12 +132,4 @@ export function EleventyDocumentOutline( } return result; }); - - /** This may not run if the build fails for some reason or another, but it - * should be okay since we use the same `tmpDir` on subsequent runs. In other - * words, the next successful run will delete the temporary directory - * properly. */ - config.events.addListener("eleventy.after", async () => { - await fs.rm(tmpDir, { recursive: true, force: true }); - }); } diff --git a/src/options.ts b/src/options.ts index 97176be..c022f5b 100644 --- a/src/options.ts +++ b/src/options.ts @@ -42,11 +42,4 @@ export type EleventyDocumentOutlineOptions = { * overwritten with a function here. This option is _not_ available on a * case-by-case basis. */ slugify?: (input: string) => string; - - /** When using the shortcode combined with a template that is not referencing - * a file (which includes the default configuration) a temporary directory is - * needed to write document outline templates to in order for them to be - * processed by Eleventy. By default, `"tmpDirEleventyDocumentOutline"` is - * used, but it is possible to overwrite this using this option. */ - tmpDir?: string; }; diff --git a/src/render-template.ts b/src/render-template.ts index c1b75a9..6cc99c9 100644 --- a/src/render-template.ts +++ b/src/render-template.ts @@ -1,26 +1,11 @@ -import fs from "node:fs/promises"; - -const templateMap = new Map<{ lang: string; source: string }, string>(); +import { RenderPlugin } from "npm:@11ty/eleventy@^3.0.0-alpha.15"; 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 { - 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); + const renderer = typeof template == "string" + ? await RenderPlugin.File(template) + : await RenderPlugin.String(template.source, template.lang); + return await renderer(data); }