feat: use go tool

This commit is contained in:
2026-07-03 19:24:01 -04:00
parent 39548b4332
commit 4d0cab1fe0
1542 changed files with 252399 additions and 14 deletions
+15
View File
@@ -0,0 +1,15 @@
/**
* Create an extension for `micromark` to support math when serializing to
* HTML.
*
* > 👉 **Note**: this uses KaTeX to render math.
*
* @param {Options | null | undefined} [options={}]
* Configuration (default: `{}`).
* @returns {HtmlExtension}
* Extension for `micromark` that can be passed in `htmlExtensions`, to
* support math when serializing to HTML.
*/
export function mathHtml(options?: Options | null | undefined): HtmlExtension;
import type { HtmlOptions as Options } from 'micromark-extension-math';
import type { HtmlExtension } from 'micromark-util-types';
+84
View File
@@ -0,0 +1,84 @@
/**
* @import {HtmlOptions as Options} from 'micromark-extension-math'
* @import {HtmlExtension} from 'micromark-util-types'
*/
import katex from 'katex';
const renderToString = katex.renderToString;
/**
* Create an extension for `micromark` to support math when serializing to
* HTML.
*
* > 👉 **Note**: this uses KaTeX to render math.
*
* @param {Options | null | undefined} [options={}]
* Configuration (default: `{}`).
* @returns {HtmlExtension}
* Extension for `micromark` that can be passed in `htmlExtensions`, to
* support math when serializing to HTML.
*/
export function mathHtml(options) {
return {
enter: {
mathFlow() {
this.lineEndingIfNeeded();
this.tag('<div class="math math-display">');
},
mathFlowFenceMeta() {
this.buffer();
},
mathText() {
// Double?
this.tag('<span class="math math-inline">');
this.buffer();
}
},
exit: {
mathFlow() {
const value = this.resume();
this.tag(math(value.replace(/(?:\r?\n|\r)$/, ''), true));
this.tag('</div>');
this.setData('mathFlowOpen');
this.setData('slurpOneLineEnding');
},
mathFlowFence() {
// After the first fence.
if (!this.getData('mathFlowOpen')) {
this.setData('mathFlowOpen', true);
this.setData('slurpOneLineEnding', true);
this.buffer();
}
},
mathFlowFenceMeta() {
this.resume();
},
mathFlowValue(token) {
this.raw(this.sliceSerialize(token));
},
mathText() {
const value = this.resume();
this.tag(math(value, false));
this.tag('</span>');
},
mathTextData(token) {
this.raw(this.sliceSerialize(token));
}
}
};
/**
* @param {string} value
* Math text.
* @param {boolean} displayMode
* Whether the math is in display mode.
* @returns {string}
* HTML.
*/
function math(value, displayMode) {
return renderToString(value, {
...options,
displayMode
});
}
}
+3
View File
@@ -0,0 +1,3 @@
/** @type {Construct} */
export const mathFlow: Construct;
import type { Construct } from 'micromark-util-types';
+345
View File
@@ -0,0 +1,345 @@
/**
* @import {Construct, State, TokenizeContext, Tokenizer} from 'micromark-util-types'
*/
import { factorySpace } from 'micromark-factory-space';
import { markdownLineEnding } from 'micromark-util-character';
/** @type {Construct} */
export const mathFlow = {
tokenize: tokenizeMathFenced,
concrete: true,
name: 'mathFlow'
};
/** @type {Construct} */
const nonLazyContinuation = {
tokenize: tokenizeNonLazyContinuation,
partial: true
};
/**
* @this {TokenizeContext}
* @type {Tokenizer}
*/
function tokenizeMathFenced(effects, ok, nok) {
const self = this;
const tail = self.events[self.events.length - 1];
const initialSize = tail && tail[1].type === "linePrefix" ? tail[2].sliceSerialize(tail[1], true).length : 0;
let sizeOpen = 0;
return start;
/**
* Start of math.
*
* ```markdown
* > | $$
* ^
* | \frac{1}{2}
* | $$
* ```
*
* @type {State}
*/
function start(code) {
effects.enter('mathFlow');
effects.enter('mathFlowFence');
effects.enter('mathFlowFenceSequence');
return sequenceOpen(code);
}
/**
* In opening fence sequence.
*
* ```markdown
* > | $$
* ^
* | \frac{1}{2}
* | $$
* ```
*
* @type {State}
*/
function sequenceOpen(code) {
if (code === 36) {
effects.consume(code);
sizeOpen++;
return sequenceOpen;
}
if (sizeOpen < 2) {
return nok(code);
}
effects.exit('mathFlowFenceSequence');
return factorySpace(effects, metaBefore, "whitespace")(code);
}
/**
* In opening fence, before meta.
*
* ```markdown
* > | $$asciimath
* ^
* | x < y
* | $$
* ```
*
* @type {State}
*/
function metaBefore(code) {
if (code === null || markdownLineEnding(code)) {
return metaAfter(code);
}
effects.enter('mathFlowFenceMeta');
effects.enter("chunkString", {
contentType: "string"
});
return meta(code);
}
/**
* In meta.
*
* ```markdown
* > | $$asciimath
* ^
* | x < y
* | $$
* ```
*
* @type {State}
*/
function meta(code) {
if (code === null || markdownLineEnding(code)) {
effects.exit("chunkString");
effects.exit('mathFlowFenceMeta');
return metaAfter(code);
}
if (code === 36) {
return nok(code);
}
effects.consume(code);
return meta;
}
/**
* After meta.
*
* ```markdown
* > | $$
* ^
* | \frac{1}{2}
* | $$
* ```
*
* @type {State}
*/
function metaAfter(code) {
// Guaranteed to be eol/eof.
effects.exit('mathFlowFence');
if (self.interrupt) {
return ok(code);
}
return effects.attempt(nonLazyContinuation, beforeNonLazyContinuation, after)(code);
}
/**
* After eol/eof in math, at a non-lazy closing fence or content.
*
* ```markdown
* | $$
* > | \frac{1}{2}
* ^
* > | $$
* ^
* ```
*
* @type {State}
*/
function beforeNonLazyContinuation(code) {
return effects.attempt({
tokenize: tokenizeClosingFence,
partial: true
}, after, contentStart)(code);
}
/**
* Before math content, definitely not before a closing fence.
*
* ```markdown
* | $$
* > | \frac{1}{2}
* ^
* | $$
* ```
*
* @type {State}
*/
function contentStart(code) {
return (initialSize ? factorySpace(effects, beforeContentChunk, "linePrefix", initialSize + 1) : beforeContentChunk)(code);
}
/**
* Before math content, after optional prefix.
*
* ```markdown
* | $$
* > | \frac{1}{2}
* ^
* | $$
* ```
*
* @type {State}
*/
function beforeContentChunk(code) {
if (code === null) {
return after(code);
}
if (markdownLineEnding(code)) {
return effects.attempt(nonLazyContinuation, beforeNonLazyContinuation, after)(code);
}
effects.enter('mathFlowValue');
return contentChunk(code);
}
/**
* In math content.
*
* ```markdown
* | $$
* > | \frac{1}{2}
* ^
* | $$
* ```
*
* @type {State}
*/
function contentChunk(code) {
if (code === null || markdownLineEnding(code)) {
effects.exit('mathFlowValue');
return beforeContentChunk(code);
}
effects.consume(code);
return contentChunk;
}
/**
* After math (ha!).
*
* ```markdown
* | $$
* | \frac{1}{2}
* > | $$
* ^
* ```
*
* @type {State}
*/
function after(code) {
effects.exit('mathFlow');
return ok(code);
}
/** @type {Tokenizer} */
function tokenizeClosingFence(effects, ok, nok) {
let size = 0;
/**
* Before closing fence, at optional whitespace.
*
* ```markdown
* | $$
* | \frac{1}{2}
* > | $$
* ^
* ```
*/
return factorySpace(effects, beforeSequenceClose, "linePrefix", self.parser.constructs.disable.null.includes('codeIndented') ? undefined : 4);
/**
* In closing fence, after optional whitespace, at sequence.
*
* ```markdown
* | $$
* | \frac{1}{2}
* > | $$
* ^
* ```
*
* @type {State}
*/
function beforeSequenceClose(code) {
effects.enter('mathFlowFence');
effects.enter('mathFlowFenceSequence');
return sequenceClose(code);
}
/**
* In closing fence sequence.
*
* ```markdown
* | $$
* | \frac{1}{2}
* > | $$
* ^
* ```
*
* @type {State}
*/
function sequenceClose(code) {
if (code === 36) {
size++;
effects.consume(code);
return sequenceClose;
}
if (size < sizeOpen) {
return nok(code);
}
effects.exit('mathFlowFenceSequence');
return factorySpace(effects, afterSequenceClose, "whitespace")(code);
}
/**
* After closing fence sequence, after optional whitespace.
*
* ```markdown
* | $$
* | \frac{1}{2}
* > | $$
* ^
* ```
*
* @type {State}
*/
function afterSequenceClose(code) {
if (code === null || markdownLineEnding(code)) {
effects.exit('mathFlowFence');
return ok(code);
}
return nok(code);
}
}
}
/**
* @this {TokenizeContext}
* @type {Tokenizer}
*/
function tokenizeNonLazyContinuation(effects, ok, nok) {
const self = this;
return start;
/** @type {State} */
function start(code) {
if (code === null) {
return ok(code);
}
effects.enter("lineEnding");
effects.consume(code);
effects.exit("lineEnding");
return lineStart;
}
/** @type {State} */
function lineStart(code) {
return self.parser.lazy[self.now().line] ? nok(code) : ok(code);
}
}
+9
View File
@@ -0,0 +1,9 @@
/**
* @param {Options | null | undefined} [options={}]
* Configuration (default: `{}`).
* @returns {Construct}
* Construct.
*/
export function mathText(options?: Options | null | undefined): Construct;
import type { Options } from 'micromark-extension-math';
import type { Construct } from 'micromark-util-types';
+232
View File
@@ -0,0 +1,232 @@
/**
* @import {Options} from 'micromark-extension-math'
* @import {Construct, Previous, Resolver, State, Token, TokenizeContext, Tokenizer} from 'micromark-util-types'
*/
// To do: next major: clean spaces in HTML compiler.
// This has to be coordinated together with `mdast-util-math`.
import { markdownLineEnding } from 'micromark-util-character';
/**
* @param {Options | null | undefined} [options={}]
* Configuration (default: `{}`).
* @returns {Construct}
* Construct.
*/
export function mathText(options) {
const options_ = options || {};
let single = options_.singleDollarTextMath;
if (single === null || single === undefined) {
single = true;
}
return {
tokenize: tokenizeMathText,
resolve: resolveMathText,
previous,
name: 'mathText'
};
/**
* @this {TokenizeContext}
* @type {Tokenizer}
*/
function tokenizeMathText(effects, ok, nok) {
const self = this;
let sizeOpen = 0;
/** @type {number} */
let size;
/** @type {Token} */
let token;
return start;
/**
* Start of math (text).
*
* ```markdown
* > | $a$
* ^
* > | \$a$
* ^
* ```
*
* @type {State}
*/
function start(code) {
effects.enter('mathText');
effects.enter('mathTextSequence');
return sequenceOpen(code);
}
/**
* In opening sequence.
*
* ```markdown
* > | $a$
* ^
* ```
*
* @type {State}
*/
function sequenceOpen(code) {
if (code === 36) {
effects.consume(code);
sizeOpen++;
return sequenceOpen;
}
// Not enough markers in the sequence.
if (sizeOpen < 2 && !single) {
return nok(code);
}
effects.exit('mathTextSequence');
return between(code);
}
/**
* Between something and something else.
*
* ```markdown
* > | $a$
* ^^
* ```
*
* @type {State}
*/
function between(code) {
if (code === null) {
return nok(code);
}
if (code === 36) {
token = effects.enter('mathTextSequence');
size = 0;
return sequenceClose(code);
}
// Tabs dont work, and virtual spaces dont make sense.
if (code === 32) {
effects.enter('space');
effects.consume(code);
effects.exit('space');
return between;
}
if (markdownLineEnding(code)) {
effects.enter("lineEnding");
effects.consume(code);
effects.exit("lineEnding");
return between;
}
// Data.
effects.enter('mathTextData');
return data(code);
}
/**
* In data.
*
* ```markdown
* > | $a$
* ^
* ```
*
* @type {State}
*/
function data(code) {
if (code === null || code === 32 || code === 36 || markdownLineEnding(code)) {
effects.exit('mathTextData');
return between(code);
}
effects.consume(code);
return data;
}
/**
* In closing sequence.
*
* ```markdown
* > | `a`
* ^
* ```
*
* @type {State}
*/
function sequenceClose(code) {
// More.
if (code === 36) {
effects.consume(code);
size++;
return sequenceClose;
}
// Done!
if (size === sizeOpen) {
effects.exit('mathTextSequence');
effects.exit('mathText');
return ok(code);
}
// More or less accents: mark as data.
token.type = 'mathTextData';
return data(code);
}
}
}
/** @type {Resolver} */
function resolveMathText(events) {
let tailExitIndex = events.length - 4;
let headEnterIndex = 3;
/** @type {number} */
let index;
/** @type {number | undefined} */
let enter;
// If we start and end with an EOL or a space.
if ((events[headEnterIndex][1].type === "lineEnding" || events[headEnterIndex][1].type === 'space') && (events[tailExitIndex][1].type === "lineEnding" || events[tailExitIndex][1].type === 'space')) {
index = headEnterIndex;
// And we have data.
while (++index < tailExitIndex) {
if (events[index][1].type === 'mathTextData') {
// Then we have padding.
events[tailExitIndex][1].type = 'mathTextPadding';
events[headEnterIndex][1].type = 'mathTextPadding';
headEnterIndex += 2;
tailExitIndex -= 2;
break;
}
}
}
// Merge adjacent spaces and data.
index = headEnterIndex - 1;
tailExitIndex++;
while (++index <= tailExitIndex) {
if (enter === undefined) {
if (index !== tailExitIndex && events[index][1].type !== "lineEnding") {
enter = index;
}
} else if (index === tailExitIndex || events[index][1].type === "lineEnding") {
events[enter][1].type = 'mathTextData';
if (index !== enter + 2) {
events[enter][1].end = events[index - 1][1].end;
events.splice(enter + 2, index - enter - 2);
tailExitIndex -= index - enter - 2;
index = enter + 2;
}
enter = undefined;
}
}
return events;
}
/**
* @this {TokenizeContext}
* @type {Previous}
*/
function previous(code) {
// If there is a previous code, there will always be a tail.
return code !== 36 || this.events[this.events.length - 1][1].type === "characterEscape";
}
+12
View File
@@ -0,0 +1,12 @@
/**
* Create an extension for `micromark` to enable math syntax.
*
* @param {Options | null | undefined} [options={}]
* Configuration (default: `{}`).
* @returns {Extension}
* Extension for `micromark` that can be passed in `extensions`, to
* enable math syntax.
*/
export function math(options?: Options | null | undefined): Extension;
import type { Options } from 'micromark-extension-math';
import type { Extension } from 'micromark-util-types';
+27
View File
@@ -0,0 +1,27 @@
/**
* @import {Options} from 'micromark-extension-math'
* @import {Extension} from 'micromark-util-types'
*/
import { mathFlow } from './math-flow.js';
import { mathText } from './math-text.js';
/**
* Create an extension for `micromark` to enable math syntax.
*
* @param {Options | null | undefined} [options={}]
* Configuration (default: `{}`).
* @returns {Extension}
* Extension for `micromark` that can be passed in `extensions`, to
* enable math syntax.
*/
export function math(options) {
return {
flow: {
[36]: mathFlow
},
text: {
[36]: mathText(options)
}
};
}