Skip to content

Commit

Permalink
It. Just. Works
Browse files Browse the repository at this point in the history
  • Loading branch information
sransara committed Jul 1, 2024
1 parent 4cc0a80 commit e647aea
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 10 deletions.
36 changes: 32 additions & 4 deletions linkd/astro-adocx/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 ?? {});
Expand All @@ -75,28 +88,37 @@ export function adocx(
return compileAdoc(asciidoctorEngine, filename, adocxConfig, asciidoctorConfig);
};

let astroFileToCompileMetadata = new Map<string, CompileMetadata>();

updateConfig({
vite: {
plugins: [
{
name: 'vite-astro-adocx',
enforce: 'pre',
configResolved(viteConfig) {
_compileAstro = (code, filename) => {
return compileAstro({
_compileAstro = async (code, filename) => {
const result = await compileAstro({
compileProps: {
astroConfig,
viteConfig,
preferences: new Map() as any,
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;
}
Expand Down Expand Up @@ -148,6 +170,12 @@ export function adocx(
throw e;
}
},
async handleHotUpdate(ctx) {
return handleHotUpdate(ctx, {
logger: logger as any,
astroFileToCompileMetadata,
});
},
},
] as VitePlugin[],
},
Expand Down
2 changes: 1 addition & 1 deletion src/lib/astro/katex/Katex.astro
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const options: KatexOptions = {
displayMode: block,
throwOnError,
fleqn: true,
output: 'html'
output: 'html',
};
const output = katex.renderToString(content, options);
Expand Down
7 changes: 4 additions & 3 deletions src/pages/notes/[year]/[slug]/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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`);
---
Expand Down
8 changes: 8 additions & 0 deletions src/text/index.ts
Original file line number Diff line number Diff line change
@@ -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<AstroInstance>,
]),
);
4 changes: 2 additions & 2 deletions src/text/notes/2019/hello-world/index.adoc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
import { Counter } from './Counter.jsx';
// import { Counter } from './Counter.jsx';
---
= Hello from another world.

Expand All @@ -24,7 +24,7 @@ The hail-and-rainbow protocol can be initiated < at five levels:
asciimath:[a^2 + b^2 = c^2]

++++
<Counter client:load={true} />
<!-- <Counter client:load={true} /> -->
++++

=== Toatts
Expand Down

0 comments on commit e647aea

Please sign in to comment.