diff --git a/_config.ts b/_config.ts index 06c53c6..1aff2ab 100644 --- a/_config.ts +++ b/_config.ts @@ -2,6 +2,7 @@ import lume from "lume/mod.ts"; import nav from "lume/plugins/nav.ts"; import codeHighlight from "lume/plugins/code_highlight.ts"; import googleFonts from "lume/plugins/google_fonts.ts"; +import { djotRender } from "./_djot.ts"; const site = lume(); @@ -18,4 +19,7 @@ site.use(googleFonts({ site.copy("/styles"); site.copy("favicon.svg") +site.data("djot", djotRender); +site.filter("djot", djotRender); + export default site; diff --git a/_djot.ts b/_djot.ts new file mode 100644 index 0000000..940a0b5 --- /dev/null +++ b/_djot.ts @@ -0,0 +1,44 @@ +import djot from "@djot/djot"; + +function extractTitle(ast: djot.Doc): string { + const firstSection = ast.children.find((c) => c.tag == "section"); + const firstHeading: djot.Heading | undefined = firstSection?.children.find( + (c) => c.tag == "heading", + ); + const headingContent = firstHeading?.children.at(0); + const title = headingContent?.tag == "str" ? headingContent.text : undefined; + return title ?? "No title"; +} + +export function djotRender(content: string): string { + return djot.renderHTML(djot.parse(content)); +} + +export default async function djotLoader(path: string | URL) { + const content = await Deno.readTextFile(path); + + const ast = djot.parse(content); + + djot.applyFilter(ast, () => { + return { + link: (el) => { + if (el.destination) { + el.destination = el.destination.replace( + /(\.dj)(#[^.\s]+)?$/, + (_match: string, _ext: string, section: string) => { + return section ? section : ""; + }, + ); + } + }, + }; + }); + + const title = extractTitle(ast); + const html = djot.renderHTML(ast); + + return { + title, + content: html, + }; +} diff --git a/deno.json b/deno.json index 6710cab..aadb79c 100644 --- a/deno.json +++ b/deno.json @@ -1,5 +1,6 @@ { "imports": { + "@djot/djot": "npm:@djot/djot@^0.3.2", "lume/": "https://deno.land/x/lume@v3.0.1/", "lume/jsx-runtime": "https://deno.land/x/ssx@v0.1.10/jsx-runtime.ts" }, diff --git a/deno.lock b/deno.lock index 584aa35..6cd4b70 100644 --- a/deno.lock +++ b/deno.lock @@ -31,6 +31,7 @@ "jsr:@std/http@1.0.12": "1.0.12", "jsr:@std/http@1.0.15": "1.0.15", "jsr:@std/io@0.225": "0.225.2", + "jsr:@std/json@^1.0.2": "1.0.2", "jsr:@std/jsonc@1.0.1": "1.0.1", "jsr:@std/jsonc@1.0.2": "1.0.2", "jsr:@std/log@0.224.13": "0.224.13", @@ -50,7 +51,9 @@ "jsr:@std/yaml@1.0.5": "1.0.5", "jsr:@std/yaml@1.0.6": "1.0.6", "jsr:@std/yaml@^1.0.5": "1.0.6", + "npm:@djot/djot@~0.3.2": "0.3.2", "npm:@js-temporal/polyfill@0.4.4": "0.4.4", + "npm:@types/estree@1.0.6": "1.0.6", "npm:estree-walker@3.0.3": "3.0.3", "npm:highlight.js@11.11.1": "11.11.1", "npm:ico-endec@0.1.6": "0.1.6", @@ -165,11 +168,17 @@ "@std/io@0.225.2": { "integrity": "3c740cd4ee4c082e6cfc86458f47e2ab7cb353dc6234d5e9b1f91a2de5f4d6c7" }, + "@std/json@1.0.2": { + "integrity": "d9e5497801c15fb679f55a2c01c7794ad7a5dfda4dd1bebab5e409cb5e0d34d4" + }, "@std/jsonc@1.0.1": { "integrity": "6b36956e2a7cbb08ca5ad7fbec72e661e6217c202f348496ea88747636710dda" }, "@std/jsonc@1.0.2": { - "integrity": "909605dae3af22bd75b1cbda8d64a32cf1fd2cf6efa3f9e224aba6d22c0f44c7" + "integrity": "909605dae3af22bd75b1cbda8d64a32cf1fd2cf6efa3f9e224aba6d22c0f44c7", + "dependencies": [ + "jsr:@std/json" + ] }, "@std/log@0.224.13": { "integrity": "f04d82f676c9eb4306194ca166d296d9f1456fe4b7edf2a404a0d55c94d31df7", @@ -214,6 +223,9 @@ } }, "npm": { + "@djot/djot@0.3.2": { + "integrity": "sha512-joMKR24B8rxueyFiJbpZAqEiypjvOyzTxzkhyr0q5mM/sUBaOD3unna/9IxtOotFugViyYlkIRaiXg3xM//zxg==" + }, "@emnapi/runtime@1.3.1": { "integrity": "sha512-kEBmG8KyqtxJZv+ygbEim+KCGtIq1fC22Ms3S4ziXmYKm8uyoLX0MHONVKwp+9opg390VaKRNt4a7A9NwmpNhw==", "dependencies": [ @@ -455,6 +467,7 @@ } }, "remote": { + "https://cdn.jsdelivr.net/gh/lumeland/bar@0.1.4/types.ts": "89d131290feb5c6781bf752525bc35d21a49612f9b3cd6b5e3a44dca15a996d6", "https://deno.land/std@0.170.0/_util/asserts.ts": "d0844e9b62510f89ce1f9878b046f6a57bf88f208a10304aab50efcb48365272", "https://deno.land/std@0.170.0/_util/os.ts": "8a33345f74990e627b9dfe2de9b040004b08ea5146c7c9e8fe9a29070d193934", "https://deno.land/std@0.170.0/encoding/base64.ts": "8605e018e49211efc767686f6f687827d7f5fd5217163e981d8d693105640d7a", @@ -764,6 +777,7 @@ "https://deno.land/x/lume@v3.0.1/deps/toml.ts": "504cb172041172b88a683f67e5ce49902563078252625a92f36a40250d68b8ed", "https://deno.land/x/lume@v3.0.1/deps/vento.ts": "52abaab33bf360365323bf4c2b1ddcf3aee604146470e917729575271eb7b8a2", "https://deno.land/x/lume@v3.0.1/deps/yaml.ts": "d0f41ff80ce1eee045a87bf055c199b5c6f316571dcad0fff99fba17e34990a2", + "https://deno.land/x/lume@v3.0.1/lint.ts": "4b369361e0cff20a8dfd9e3ff8cb642aa805e7532825ea3a5378eb1f80901fc6", "https://deno.land/x/lume@v3.0.1/middlewares/logger.ts": "c96f1a9f9d5757555b6f141865ce8551ac176f90c8ee3e9ad797b2b400a9a567", "https://deno.land/x/lume@v3.0.1/middlewares/no_cache.ts": "0119e3ae3a596ab12c42df693b93e5b03dd9608e289d862242751a9739438f35", "https://deno.land/x/lume@v3.0.1/middlewares/no_cors.ts": "4d24619b5373c98bcc3baf404db47ba088c87ac8538ea1784e58d197b81d4d02", @@ -783,6 +797,7 @@ "https://deno.land/x/lume@v3.0.1/plugins/url.ts": "15f2e80b6fcbf86f8795a3676b8d533bab003ac016ff127e58165a6ac3bffc1a", "https://deno.land/x/lume@v3.0.1/plugins/vento.ts": "d74100c7168d49b3817559792c9a8fd1d380b3244051d92dfebc7531cb8b7665", "https://deno.land/x/lume@v3.0.1/plugins/yaml.ts": "d0ebf37c38648172c6b95c502753a3edf60278ab4f6a063f3ca00f31e0dd90cc", + "https://deno.land/x/lume@v3.0.1/types.ts": "5f580502f366b9b25106eb72d49b30d9af7715c8a304fe6e21f382d3c2a4cc38", "https://deno.land/x/lume_init@v0.4.0/deps.ts": "51061dea0c2f1cb2a326062c6cdd531dfaa6b8c9692e7f786cef2eef709e612a", "https://deno.land/x/lume_init@v0.4.0/init.ts": "82f2b2846651a1239c7a89c9a83e7c2eb95032413b9817d267066fcabae26c9c", "https://deno.land/x/lume_init@v0.4.0/mod.ts": "8f2731c650153c640e74e693c212fe30ae32d6d55ae636397f6644b3ccc45907", @@ -797,6 +812,9 @@ "https://deno.land/x/lume_init@v0.4.0/steps/update.ts": "1271c0d6895dfb2acf1495a36676e4f1c454e9ba090a85bf88835e0756f26d3a", "https://deno.land/x/lume_init@v0.4.0/steps/utils.ts": "fa1eba2ae29193395465022119069b322f8164c3064bbdd9e0664cb385f00e7b", "https://deno.land/x/lume_init@v0.4.0/upgrade.ts": "2a5ca0b4ae0db1f6771e6ec4cede420b6c267e882a38e5647bc7218fda862483", + "https://deno.land/x/ssx@v0.1.10/css.ts": "39972fa9e375465b82e4fbf735dcc727acc89fdd836f93a395cfb3ccab54e7f0", + "https://deno.land/x/ssx@v0.1.10/html.ts": "5ad7bfd7a6a5b676b2686d406c105bbb02bea537183d95e0c04e76853a9ee155", + "https://deno.land/x/ssx@v0.1.10/jsx-runtime.ts": "f3d37c172698f0b0d7510c2023119264057cbc64a5602d4ca9091e80199a2abe", "https://deno.land/x/vento@v1.12.15/deps.ts": "155958dfada8d8cb3c8a001413c759928647b23e0e9db25195614549b58d085f", "https://deno.land/x/vento@v1.12.15/mod.ts": "cfaac455f70af8e59aa0c03ef39b641635094225255f0fbaa76f4771e683f2ca", "https://deno.land/x/vento@v1.12.15/plugins/auto_trim.ts": "503137c3f5cec20e0c491d7963b0dc310de1a6a2e74d41913bbf6475eb1c807e", @@ -842,5 +860,10 @@ "https://deno.land/x/vento@v1.12.16/src/loader.ts": "c05add67f582e937ee611852075ce2cc038b5e80e3e609eef96fa5ed74a5086c", "https://deno.land/x/vento@v1.12.16/src/tokenizer.ts": "127ddad02054f63b8b646e4dfbf555e1e34e9b8dcbd58d86b3729a4de95abd27", "https://deno.land/x/vento@v1.12.16/src/transformer.ts": "9ff70c554b3889151745b5f7117bc5c02b889e6e16ca53e5b8c1fa6b767fb451" + }, + "workspace": { + "dependencies": [ + "npm:@djot/djot@~0.3.2" + ] } }