mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-30 22:39:58 +02:00
Add build time math rendering
While very useful on its own (and combined with the passthrough render hooks), this also serves as a proof of concept of using WASI (WebAssembly System Interface) modules in Hugo. This will be marked _experimental_ in the documentation. Not because it will be removed or changed in a dramatic way, but we need to think a little more how to best set up/configure similar services, define where these WASM files gets stored, maybe we can allow user provided WASM files plugins via Hugo Modules mounts etc. See these issues for more context: * https://github.com/gohugoio/hugo/issues/12736 * https://github.com/gohugoio/hugo/issues/12737 See #11927
This commit is contained in:
2
internal/warpc/js/.gitignore
vendored
Normal file
2
internal/warpc/js/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
node_modules/
|
||||
package-lock.json
|
56
internal/warpc/js/common.js
Normal file
56
internal/warpc/js/common.js
Normal file
@@ -0,0 +1,56 @@
|
||||
// Read JSONL from stdin.
|
||||
export function readInput(handle) {
|
||||
const buffSize = 1024;
|
||||
let currentLine = [];
|
||||
const buffer = new Uint8Array(buffSize);
|
||||
|
||||
// Read all the available bytes
|
||||
while (true) {
|
||||
// Stdin file descriptor
|
||||
const fd = 0;
|
||||
let bytesRead = 0;
|
||||
try {
|
||||
bytesRead = Javy.IO.readSync(fd, buffer);
|
||||
} catch (e) {
|
||||
// IO.readSync fails with os error 29 when stdin closes.
|
||||
if (e.message.includes('os error 29')) {
|
||||
break;
|
||||
}
|
||||
throw new Error('Error reading from stdin');
|
||||
}
|
||||
|
||||
if (bytesRead < 0) {
|
||||
throw new Error('Error reading from stdin');
|
||||
break;
|
||||
}
|
||||
|
||||
if (bytesRead === 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
currentLine = [...currentLine, ...buffer.subarray(0, bytesRead)];
|
||||
|
||||
// Split array into chunks by newline.
|
||||
let i = 0;
|
||||
for (let j = 0; i < currentLine.length; i++) {
|
||||
if (currentLine[i] === 10) {
|
||||
const chunk = currentLine.splice(j, i + 1);
|
||||
const arr = new Uint8Array(chunk);
|
||||
const json = JSON.parse(new TextDecoder().decode(arr));
|
||||
handle(json);
|
||||
j = i + 1;
|
||||
}
|
||||
}
|
||||
// Remove processed data.
|
||||
currentLine = currentLine.slice(i);
|
||||
}
|
||||
}
|
||||
|
||||
// Write JSONL to stdout
|
||||
export function writeOutput(output) {
|
||||
const encodedOutput = new TextEncoder().encode(JSON.stringify(output) + '\n');
|
||||
const buffer = new Uint8Array(encodedOutput);
|
||||
// Stdout file descriptor
|
||||
const fd = 1;
|
||||
Javy.IO.writeSync(fd, buffer);
|
||||
}
|
2
internal/warpc/js/greet.bundle.js
Normal file
2
internal/warpc/js/greet.bundle.js
Normal file
@@ -0,0 +1,2 @@
|
||||
(()=>{function i(r){let e=[],a=new Uint8Array(1024);for(;;){let n=0;try{n=Javy.IO.readSync(0,a)}catch(o){if(o.message.includes("os error 29"))break;throw new Error("Error reading from stdin")}if(n<0)throw new Error("Error reading from stdin");if(n===0)break;e=[...e,...a.subarray(0,n)];let t=0;for(let o=0;t<e.length;t++)if(e[t]===10){let f=e.splice(o,t+1),s=new Uint8Array(f),u=JSON.parse(new TextDecoder().decode(s));r(u),o=t+1}e=e.slice(t)}}function d(r){let c=new TextEncoder().encode(JSON.stringify(r)+`
|
||||
`),e=new Uint8Array(c);Javy.IO.writeSync(1,e)}var l=function(r){d({header:r.header,data:{greeting:"Hello "+r.data.name+"!"}})};console.log("Greet module loaded");i(l);})();
|
9
internal/warpc/js/greet.js
Normal file
9
internal/warpc/js/greet.js
Normal file
@@ -0,0 +1,9 @@
|
||||
import { readInput, writeOutput } from './common';
|
||||
|
||||
const greet = function (input) {
|
||||
writeOutput({ header: input.header, data: { greeting: 'Hello ' + input.data.name + '!' } });
|
||||
};
|
||||
|
||||
console.log('Greet module loaded');
|
||||
|
||||
readInput(greet);
|
14
internal/warpc/js/package.json
Normal file
14
internal/warpc/js/package.json
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "js",
|
||||
"version": "1.0.0",
|
||||
"main": "greet.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"description": "",
|
||||
"devDependencies": {
|
||||
"katex": "^0.16.11"
|
||||
}
|
||||
}
|
262
internal/warpc/js/renderkatex.bundle.js
Normal file
262
internal/warpc/js/renderkatex.bundle.js
Normal file
File diff suppressed because one or more lines are too long
11
internal/warpc/js/renderkatex.js
Normal file
11
internal/warpc/js/renderkatex.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import { readInput, writeOutput } from './common';
|
||||
import katex from 'katex';
|
||||
|
||||
const render = function (input) {
|
||||
const data = input.data;
|
||||
const expression = data.expression;
|
||||
const options = data.options;
|
||||
writeOutput({ header: input.header, data: { output: katex.renderToString(expression, options) } });
|
||||
};
|
||||
|
||||
readInput(render);
|
Reference in New Issue
Block a user