diff --git a/linkd/astro-adocx/integration.ts b/linkd/astro-adocx/integration.ts index 703ded5..5193fa9 100644 --- a/linkd/astro-adocx/integration.ts +++ b/linkd/astro-adocx/integration.ts @@ -3,11 +3,14 @@ import fs from 'node:fs'; import type { Asciidoctor, ProcessorOptions } from 'asciidoctor'; import asciidoctor from 'asciidoctor'; import type { AstroIntegration } from 'astro'; +import astroJSXRenderer from 'astro/jsx/renderer.js'; import type { Plugin as VitePlugin } from 'vite'; import { compileAstro, type CompileAstroResult, } from './node_modules/astro/dist/vite-plugin-astro/compile.js'; +import { handleHotUpdate } from './node_modules/astro/dist/vite-plugin-astro/hmr.js'; +import { type CompileMetadata } from './node_modules/astro/dist/vite-plugin-astro/types.js'; import { register as converterRegisterHandle } from './converter.ts'; import subSpecialchars from './patches/sub_specialchars'; @@ -55,7 +58,17 @@ export function adocx( return { name: '@sransara/astro-adocx', hooks: { - async 'astro:config:setup'({ config: astroConfig, updateConfig, logger }) { + async 'astro:config:setup'({ + config: astroConfig, + updateConfig, + // @ts-expect-error: `addPageExtension` is part of the private api + addPageExtension, + addRenderer, + logger, + }) { + addRenderer(astroJSXRenderer); + addPageExtension(adocxExtension); + const asciidoctorEngine = asciidoctor(); subSpecialchars.patch(); converterRegisterHandle(asciidoctorEngine, adocxConfig.templates ?? {}); @@ -75,6 +88,8 @@ export function adocx( return compileAdoc(asciidoctorEngine, filename, adocxConfig, asciidoctorConfig); }; + let astroFileToCompileMetadata = new Map(); + updateConfig({ vite: { plugins: [ @@ -82,8 +97,8 @@ export function adocx( name: 'vite-astro-adocx', enforce: 'pre', configResolved(viteConfig) { - _compileAstro = (code, filename) => { - return compileAstro({ + _compileAstro = async (code, filename) => { + const result = await compileAstro({ compileProps: { astroConfig, viteConfig, @@ -91,12 +106,19 @@ export function adocx( filename, source: code, }, - astroFileToCompileMetadata: new Map(), + astroFileToCompileMetadata, logger: logger as any, }); + return result; }; }, + buildStart() { + astroFileToCompileMetadata = new Map(); + }, async load(fileId) { + if (!fileId.includes(adocxExtension)) { + return; + } if (!fileId.endsWith(adocxExtension)) { return; } @@ -148,6 +170,12 @@ export function adocx( throw e; } }, + async handleHotUpdate(ctx) { + return handleHotUpdate(ctx, { + logger: logger as any, + astroFileToCompileMetadata, + }); + }, }, ] as VitePlugin[], }, diff --git a/src/lib/astro/katex/Katex.astro b/src/lib/astro/katex/Katex.astro index c3ceae2..c154cad 100644 --- a/src/lib/astro/katex/Katex.astro +++ b/src/lib/astro/katex/Katex.astro @@ -20,7 +20,7 @@ const options: KatexOptions = { displayMode: block, throwOnError, fleqn: true, - output: 'html' + output: 'html', }; const output = katex.renderToString(content, options); diff --git a/src/pages/notes/[year]/[slug]/index.astro b/src/pages/notes/[year]/[slug]/index.astro index 5144ce2..2027f00 100644 --- a/src/pages/notes/[year]/[slug]/index.astro +++ b/src/pages/notes/[year]/[slug]/index.astro @@ -7,6 +7,7 @@ import { Image } from 'astro:assets'; import BaseLayout from '@/src/layouts/baseLayout/BaseLayout.astro'; import Asciidoc from '@/src/lib/astro/asciidoc/Asciidoc.astro'; import NavBar from '@/src/lib/astro/nav-bar/NavBar.astro'; +import { entries } from '@/src/text'; export function getStaticPaths() { const metadataPaths = import.meta.glob('/src/text/notes/**/metadata.ts'); @@ -35,9 +36,9 @@ if (!metadata.publishedDate.startsWith(year || '0000')) { throw new Error("metadata.publishedDate and path year doesn't match"); } -const AstroEmbed = (await import( - `../../../../../src/text/notes/${year}/${slug}/index.adoc` -)) as AstroInstance & { docattrs: any }; +const AstroEmbed = (await entries[`notes/${year}/${slug}/index.adoc`]()) as AstroInstance & { + docattrs: any; +}; const poster = import(`../../../../../src/text/notes/${year}/${slug}/poster.jpg`); --- diff --git a/src/text/index.ts b/src/text/index.ts new file mode 100644 index 0000000..2322983 --- /dev/null +++ b/src/text/index.ts @@ -0,0 +1,8 @@ +import type { AstroInstance } from 'astro'; + +export const entries = Object.fromEntries( + Object.entries(import.meta.glob('./**/*.adoc')).map(([key, value]) => [ + key.replace(/^\.\//, ''), + value as () => Promise, + ]), +); diff --git a/src/text/notes/2019/hello-world/index.adoc b/src/text/notes/2019/hello-world/index.adoc index 70a06e9..49fd1a3 100644 --- a/src/text/notes/2019/hello-world/index.adoc +++ b/src/text/notes/2019/hello-world/index.adoc @@ -1,5 +1,5 @@ --- -import { Counter } from './Counter.jsx'; +// import { Counter } from './Counter.jsx'; --- = Hello from another world. @@ -24,7 +24,7 @@ The hail-and-rainbow protocol can be initiated < at five levels: asciimath:[a^2 + b^2 = c^2] ++++ - + ++++ === Toatts