From 2b10ca41b3e07ae06659fe5b8910717073d0f52b Mon Sep 17 00:00:00 2001 From: Foster Hangdaan Date: Thu, 7 Nov 2024 20:24:21 -0500 Subject: [PATCH] Initial commit --- .editorconfig | 15 ++ .gitattributes | 219 +++++++++++++++++++++++++++ .gitignore | 5 + LICENSE.txt | 201 ++++++++++++++++++++++++ README.org | 19 +++ deno.json | 16 ++ deno.lock | Bin 0 -> 23873 bytes mod.ts | 112 ++++++++++++++ resources/diagrams.json | 170 +++++++++++++++++++++ tests/__snapshots__/mod.test.ts.snap | 23 +++ tests/mod.test.ts | 86 +++++++++++ tests/site/index.md | 27 ++++ tests/utils.ts | 59 ++++++++ 13 files changed, 952 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 LICENSE.txt create mode 100644 README.org create mode 100644 deno.json create mode 100644 deno.lock create mode 100644 mod.ts create mode 100644 resources/diagrams.json create mode 100644 tests/__snapshots__/mod.test.ts.snap create mode 100644 tests/mod.test.ts create mode 100644 tests/site/index.md create mode 100644 tests/utils.ts diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..baaff8e --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +# EditorConfig helps maintain consistent coding styles for multiple developers +# working on the same project across various editors and IDEs. +# +# https://editorconfig.org + +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 2 +indent_style = space +insert_final_newline = true +tab_width = 2 +trim_trailing_whitespace = true diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..ace87df --- /dev/null +++ b/.gitattributes @@ -0,0 +1,219 @@ +## GITATTRIBUTES FOR WEB PROJECTS +# +# These settings are for any web project. +# +# Details per file setting: +# text These files should be normalized (i.e. convert CRLF to LF). +# binary These files are binary and should be left untouched. +# +# Note that binary is a macro for -text -diff. +###################################################################### + +# Auto detect +## Handle line endings automatically for files detected as +## text and leave all files detected as binary untouched. +## This will handle all files NOT defined below. +* text=auto + +# Source code +*.bash text eol=lf +*.bat text eol=crlf +*.cmd text eol=crlf +*.coffee text +*.css text diff=css +*.htm text diff=html +*.html text diff=html +*.inc text +*.ini text +*.js text +*.mjs text +*.cjs text +*.json text +*.jsx text +*.less text +*.ls text +*.map text -diff +*.od text +*.onlydata text +*.php text diff=php +*.pl text +*.ps1 text eol=crlf +*.py text diff=python +*.rb text diff=ruby +*.sass text +*.scm text +*.scss text diff=css +*.sh text eol=lf +.husky/* text eol=lf +*.sql text +*.styl text +*.tag text +*.ts text +*.tsx text +*.xml text +*.xhtml text diff=html + +# Docker +Dockerfile text + +# Documentation +*.ipynb text eol=lf +*.markdown text diff=markdown +*.md text diff=markdown +*.mdwn text diff=markdown +*.mdown text diff=markdown +*.mkd text diff=markdown +*.mkdn text diff=markdown +*.mdtxt text +*.mdtext text +*.txt text +AUTHORS text +CHANGELOG text +CHANGES text +CONTRIBUTING text +COPYING text +copyright text +*COPYRIGHT* text +INSTALL text +license text +LICENSE text +NEWS text +readme text +*README* text +TODO text + +# Templates +*.dot text +*.ejs text +*.erb text +*.haml text +*.handlebars text +*.hbs text +*.hbt text +*.jade text +*.latte text +*.mustache text +*.njk text +*.phtml text +*.svelte text +*.tmpl text +*.tpl text +*.twig text +*.vue text + +# Configs +*.cnf text +*.conf text +*.config text +.editorconfig text +.env text +.gitattributes text +.gitconfig text +.htaccess text +*.lock text -diff +package.json text eol=lf +package-lock.json text eol=lf -diff +pnpm-lock.yaml text eol=lf -diff +.prettierrc text +yarn.lock text -diff +*.toml text +*.yaml text +*.yml text +browserslist text +Makefile text +makefile text +# Fixes syntax highlighting on GitHub to allow comments +tsconfig.json linguist-language=JSON-with-Comments + +# Heroku +Procfile text + +# Graphics +*.ai binary +*.avif binary +*.bmp binary +*.eps binary +*.gif binary +*.gifv binary +*.ico binary +*.jng binary +*.jp2 binary +*.jpeg binary +*.jpg binary +*.jpx binary +*.jxl binary +*.jxr binary +*.pdf binary +*.png binary +*.psb binary +*.psd binary +# SVG treated as an asset (binary) by default. +*.svg text +# If you want to treat it as binary, +# use the following line instead. +# *.svg binary +*.svgz binary +*.tif binary +*.tiff binary +*.wbmp binary +*.webp binary + +# Audio +*.kar binary +*.m4a binary +*.mid binary +*.midi binary +*.mp3 binary +*.ogg binary +*.ra binary + +# Video +*.3gpp binary +*.3gp binary +*.as binary +*.asf binary +*.asx binary +*.avi binary +*.fla binary +*.flv binary +*.m4v binary +*.mng binary +*.mov binary +*.mp4 binary +*.mpeg binary +*.mpg binary +*.ogv binary +*.swc binary +*.swf binary +*.webm binary + +# Archives +*.7z binary +*.gz binary +*.jar binary +*.rar binary +*.tar binary +*.zip binary + +# Fonts +*.ttf binary +*.eot binary +*.otf binary +*.woff binary +*.woff2 binary + +# Executables +*.exe binary +*.pyc binary +# Prevents massive diffs caused by vendored, minified files +**/.yarn/releases/** binary +**/.yarn/plugins/** binary + +# RC files (like .babelrc or .eslintrc) +*.*rc text + +# Ignore files (like .npmignore or .gitignore) +*.*ignore text + +# Prevents massive diffs from built files +dist/* binary diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0d7ee8d --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +# Emacs +.dir-locals-2.el + +# Lume +_site/ diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..261eeb9 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/README.org b/README.org new file mode 100644 index 0000000..848dfd0 --- /dev/null +++ b/README.org @@ -0,0 +1,19 @@ +#+title: Kroki Plugin for Lume + +* About + +A [[https://lume.land/][Lume]] plugin which utilizes [[https://kroki.io/][Kroki]] to generate images from diagram code. + +* Documentation + +For documentation, refer to [[https://code.fosterhangdaan.com/foster/lume-plugin-kroki/wiki][the wiki]]. + +* License + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with the License. You may obtain a copy of the License at [[http://www.apache.org/licenses/LICENSE-2.0]]. + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. + +* Copyright + +Copyright \copy 2024 [[https://www.fosterhangdaan.com][Foster Hangdaan]] diff --git a/deno.json b/deno.json new file mode 100644 index 0000000..8611d4c --- /dev/null +++ b/deno.json @@ -0,0 +1,16 @@ +{ + "version": "1.0.0", + "exports": "./mod.ts", + "tasks": { + "check": "deno fmt --check && deno lint && deno check .", + "test": "deno test --allow-read=. --allow-write=./tests/site/_site,./tests/__snapshots__ --deny-env" + }, + "imports": { + "@/": "./", + "@std/assert": "jsr:@std/assert@1.0.7", + "@std/path": "jsr:@std/path@1.0.8", + "@std/testing": "jsr:@std/testing@1.0.4", + "@valibot/valibot": "jsr:@valibot/valibot@0.42.1", + "lume/": "https://deno.land/x/lume@v2.4.0/" + } +} diff --git a/deno.lock b/deno.lock new file mode 100644 index 0000000000000000000000000000000000000000..1374c8a26b6ddd269e0e84b6aedbaa9cb6163afa GIT binary patch literal 23873 zcmbuH=~5g^vaSF3Q*7IRxe}R?5s{f~cFY7q2n3P zVHgG@ejs~W-u=LXHx_>Hf0q8DYqy%w?x&kZyMOiJQ0cv9oF%@}Yqx_NvCG5Tud2DV zxtX`q0_x@%<~PO-uKS~2Gu2}L%d>AhoNeg3p1(bl?Ct!pf4ud?cbnxd`Q_NTJ;wRj z-oN^Axb$8#l9*fIi@vhND1%;i^rmA*q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$P64WsLMkdo&fk>& zN$mT_pwo>G?({Bfq7o+LQ>Q2k05)U^m@x6W(8=He)JrJ9@S_x|ad7xP9#R9H2Ezkp z&F>!i@+gzLX!M~o{VA7A{hxXu9C@7tCNLIQIH!$_3znw9XHhUgFHE?^*^+W5>I=Y< zgECUf_LJUx(fC(9>uib*CFwLs1aTn`f(jI(I9F&~NdW?u_DQ(5UOEa!W0k%q9_ zb)aFuD@Qi&H4`|Sy1MkxgVZR1(8N#@=FuorDEL<@gGq0TI!40Q63zgLB^(P}isdN~ z(5VT>t0#fPg2lJZb#FY~d}L6RaqzAhjs?q-+%_JxfZ~R-3biE)MO<>SLFuXDz&POo zVSfw}UGfC3*Ni_5)4`Lq~k|Z(E=s~eh~Y{_^IH+B~dJ|L2eMbBy5-*aUbXg z7a=2#$@}Ip9C6tR+*!Q?zK}eCUDCtlNOQS>d3xd(HC!UFOie5lRM;LK5Cp|m>-6-b zq%5$d48@a)(jyC>6aE2ESO9JfOo*BhCwE()p+<+W53Z+R`H25fXQC{JPOVATi3Mnu z2zYmo&w5FuO`AUIorf;{A$N7NrZfQ0mVsjs-c(kjv4>Q|`y)n!XQHZR;eL|d8o>yO z1a0?87nYtP^>in~BTF9*Om|46qkJ`S_elpq$c8}_SRS!HiB2J~lUx~>Pi~GK(6|HZ zhjAU*Sqbby6g?khdh3Krj-g1_t--l@1OBC_sH;Rujc*RiM5$L dDEHx;^AD4me}BP)Kbp?3|4yA>|N76r{vT=rT#5hy literal 0 HcmV?d00001 diff --git a/mod.ts b/mod.ts new file mode 100644 index 0000000..14061aa --- /dev/null +++ b/mod.ts @@ -0,0 +1,112 @@ +/* + * Copyright 2024 Foster Hangdaan + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import * as v from "@valibot/valibot"; +import diagrams from "@/resources/diagrams.json" with { type: "json" }; +import type Site from "lume/core/site.ts"; +import { deflateSync } from "node:zlib"; + +const textEncoder = new TextEncoder(); + +const diagramNames: string[] = diagrams.map((d) => d.name); + +const OptionsSchema = v.optional( + v.object({ + // The names of diagrams that will be converted. + enabledDiagrams: v.optional( + v.pipe( + v.array(v.string()), + v.everyItem( + (item) => diagramNames.includes(item), + `Invalid names provided to \`enabledDiagrams\`. Valid names are: ${ + diagramNames.join(", ") + }`, + ), + ), + diagramNames, + ), + // Preferred output format. + // Falls back to `svg` if the diagram does not support it. + // For a complete list, refer to https://kroki.io/#support + format: v.optional( + v.union([ + v.literal("jpeg"), + v.literal("png"), + v.literal("svg"), + ]), + "svg", + ), + server: v.optional( + v.pipe( + v.string(), + v.nonEmpty(), + v.url(), + ), + "https://kroki.io", + ), + }), + { + enabledDiagrams: diagramNames, + format: "svg", + server: "https://kroki.io", + }, +); + +export type Options = v.InferInput; + +export default function (options?: Options): (site: Site) => void { + const opts = v.parse(OptionsSchema, options); + + return (site: Site) => { + site.process([".html"], (pages) => { + pages.forEach((page) => { + diagrams.forEach((diagram) => { + if (page.document && opts.enabledDiagrams.includes(diagram.name)) { + const format = diagram.formats.includes(opts.format) + ? opts.format + : "svg"; + + const codeblocks = page.document.querySelectorAll( + `pre > code.${diagram.matcher}`, + ); + + for (const codeblock of codeblocks) { + if ( + codeblock.textContent && codeblock.parentElement + ) { + const encoded = textEncoder.encode(codeblock.textContent); + + const compressed = deflateSync(encoded); + + const result = compressed.toString("base64url"); + + const img = page.document.createElement("img"); + + const url = new URL( + `${opts.server}/${diagram.slug}/${format}/${result}`, + ); + + img.setAttribute("src", url.toString()); + + codeblock.parentElement.replaceWith(img); + } + } + } + }); + }); + }); + }; +} diff --git a/resources/diagrams.json b/resources/diagrams.json new file mode 100644 index 0000000..9d804e6 --- /dev/null +++ b/resources/diagrams.json @@ -0,0 +1,170 @@ +[ + { + "name": "BlockDiag", + "slug": "blockdiag", + "matcher": "language-blockdiag", + "formats": ["svg", "png"] + }, + { + "name": "BPMN", + "slug": "bpmn", + "matcher": "language-bpmn", + "formats": ["svg"] + }, + { + "name": "Bytefield", + "slug": "bytefield", + "matcher": "language-bytefield", + "formats": ["svg"] + }, + { + "name": "SeqDiag", + "slug": "seqdiag", + "matcher": "language-seqdiag", + "formats": ["svg", "png"] + }, + { + "name": "ActDiag", + "slug": "actdiag", + "matcher": "language-actdiag", + "formats": ["svg", "png"] + }, + { + "name": "NwDiag", + "slug": "nwdiag", + "matcher": "language-nwdiag", + "formats": ["svg", "png"] + }, + { + "name": "PacketDiag", + "slug": "packetdiag", + "matcher": "language-packetdiag", + "formats": ["svg", "png"] + }, + { + "name": "RackDiag", + "slug": "rackdiag", + "matcher": "language-rackdiag", + "formats": ["svg", "png"] + }, + { + "name": "C4 with PlantUML", + "slug": "c4plantuml", + "matcher": "language-c4plantuml", + "formats": ["svg", "png"] + }, + { + "name": "D2", + "slug": "d2", + "matcher": "language-d2", + "formats": ["svg"] + }, + { + "name": "DBML", + "slug": "dbml", + "matcher": "language-dbml", + "formats": ["svg"] + }, + { + "name": "Ditaa", + "slug": "ditaa", + "matcher": "language-ditaa", + "formats": ["svg", "png"] + }, + { + "name": "Erd", + "slug": "erd", + "matcher": "language-erd", + "formats": ["svg", "png", "jpeg"] + }, + { + "name": "Excalidraw", + "slug": "excalidraw", + "matcher": "language-excalidraw", + "formats": ["svg"] + }, + { + "name": "GraphViz", + "slug": "graphviz", + "matcher": "language-graphviz", + "formats": ["svg", "png", "jpeg"] + }, + { + "name": "Mermaid", + "slug": "mermaid", + "matcher": "language-mermaid", + "formats": ["svg", "png"] + }, + { + "name": "Nomnoml", + "slug": "nomnoml", + "matcher": "language-nomnoml", + "formats": ["svg"] + }, + { + "name": "Pikchr", + "slug": "pikchr", + "matcher": "language-pikchr", + "formats": ["svg"] + }, + { + "name": "PlantUML", + "slug": "plantuml", + "matcher": "language-plantuml", + "formats": ["svg", "png"] + }, + { + "name": "Structurizr", + "slug": "structurizr", + "matcher": "language-structurizr", + "formats": ["svg", "png"] + }, + { + "name": "Svgbob", + "slug": "svgbob", + "matcher": "language-svgbob", + "formats": ["svg"] + }, + { + "name": "Symbolator", + "slug": "symbolator", + "matcher": "language-symbolator", + "formats": ["svg"] + }, + { + "name": "TikZ", + "slug": "tikz", + "matcher": "language-tikz", + "formats": ["svg", "png", "jpeg"] + }, + { + "name": "UMlet", + "slug": "umlet", + "matcher": "language-umlet", + "formats": ["svg", "png", "jpeg"] + }, + { + "name": "Vega", + "slug": "vega", + "matcher": "language-vega", + "formats": ["svg", "png"] + }, + { + "name": "Vega-Lite", + "slug": "vegalite", + "matcher": "language-vegalite", + "formats": ["svg", "png"] + }, + { + "name": "WaveDrom", + "slug": "wavedrom", + "matcher": "language-wavedrom", + "formats": ["svg"] + }, + { + "name": "WireViz", + "slug": "wireviz", + "matcher": "language-wireviz", + "formats": ["svg", "png"] + } +] diff --git a/tests/__snapshots__/mod.test.ts.snap b/tests/__snapshots__/mod.test.ts.snap new file mode 100644 index 0000000..72669a2 --- /dev/null +++ b/tests/__snapshots__/mod.test.ts.snap @@ -0,0 +1,23 @@ +export const snapshot = {}; + +snapshot[`Lume Site 1`] = ` +{ + content: [ + ' +

Markdown Test Page

+
no convert
+
+
div { display: none; }
+
+ + +', + ], + src: [ + { + ext: ".md", + path: "/index", + }, + ], +} +`; diff --git a/tests/mod.test.ts b/tests/mod.test.ts new file mode 100644 index 0000000..1590825 --- /dev/null +++ b/tests/mod.test.ts @@ -0,0 +1,86 @@ +/* + * Copyright 2024 Foster Hangdaan + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import mod from "@/mod.ts"; +import { ValiError } from "@valibot/valibot"; +import { assertExists, assertInstanceOf } from "@std/assert"; +import { assertSnapshot } from "@std/testing/snapshot"; +import { getSite } from "@/tests/utils.ts"; + +Deno.test("Error thrown on invalid options", async (t) => { + await t.step("Invalid server", () => { + let error; + try { + mod({ server: "invalid domain" }); + } catch (err) { + error = err; + } + assertExists(error); + assertInstanceOf(error, ValiError); + }); + + await t.step("Invalid enabledDiagrams", () => { + let error; + try { + mod({ enabledDiagrams: ["invalid diagram"] }); + } catch (err) { + error = err; + } + assertExists(error); + assertInstanceOf(error, ValiError); + }); + + await t.step("Invalid format", () => { + let error; + try { + // @ts-ignore: This is supposed to be an invalid value. + mod({ format: "invalid" }); + } catch (err) { + error = err; + } + assertExists(error); + assertInstanceOf(error, ValiError); + }); +}); + +Deno.test("Lume Site", async (t) => { + const site = getSite(); + + site.use(mod()); + + // Don't save the site to disk. + site.addEventListener("beforeSave", () => false); + + await site.build(); + + site.pages.sort((a, b) => { + if (a.outputPath > b.outputPath) { + return 1; + } else if (a.outputPath < b.outputPath) { + return -1; + } else { + return 0; + } + }); + + await assertSnapshot(t, { + src: site.pages.map((page) => ({ + path: page.src.path, + ext: page.src.ext, + })), + content: site.pages.map((page) => page.content), + }); +}); diff --git a/tests/site/index.md b/tests/site/index.md new file mode 100644 index 0000000..8d0ef20 --- /dev/null +++ b/tests/site/index.md @@ -0,0 +1,27 @@ +# Markdown Test Page + +``` +no convert +``` + +```css +div { display: none; } +``` + +```blockdiag +blockdiag { + Kroki -> generates -> "Block diagrams"; + Kroki -> is -> "very easy!"; + + Kroki [color = "greenyellow"]; + "Block diagrams" [color = "pink"]; + "very easy!" [color = "orange"]; +} +``` + +```plantuml +@startuml +Alice -> Bob: Hi! +Bob -> Alice: Hello! +@enduml +``` diff --git a/tests/utils.ts b/tests/utils.ts new file mode 100644 index 0000000..9294659 --- /dev/null +++ b/tests/utils.ts @@ -0,0 +1,59 @@ +/* + * Copyright 2024 Foster Hangdaan + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import lume, { type PluginOptions } from "lume/mod.ts"; +import type Site from "lume/core/site.ts"; +import type { DeepPartial } from "lume/core/utils/object.ts"; +import type { SiteOptions } from "lume/core/site.ts"; +import type { Writer } from "lume/core/writer.ts"; +import { fromFileUrl, join } from "@std/path"; + +const cwd = fromFileUrl(import.meta.resolve("./")); + +class TestWriter implements Writer { + savePages() { + return Promise.resolve([]); + } + + copyFiles() { + return Promise.resolve([]); + } + + clear(): Promise { + return Promise.resolve(); + } + + removeFiles(): Promise { + return Promise.resolve(); + } +} + +export function getSite( + siteOptions: DeepPartial = {}, + pluginOptions: PluginOptions = {}, +): Site { + siteOptions.cwd = join(cwd, "site"); + + const site = lume( + siteOptions, + pluginOptions, + false, + ); + + site.writer = new TestWriter(); + + return site; +}