* [packages][m]: mv @flowershow/core package here * [packages/core][xs]: rename to @portaljs/core * [package.json][xs]: setup npm workspaces * [packages/core][xs]:replace deprecated rollup executor * [core/package.json][s]: fix mermaid versions * [core/tsconfig][xs]: rm extends * [core/jest.config][xs]: rm coverageDirectory * [core/package.json][xs]: install core-js * [packages.json][s]:use same version for all nrwl packages * [core/.eslintrc][xs]: adjust ignorePatterns * [core/project.json][xs]: rm publish targets * [packages][m]: mv @flowershow/remark-wiki-link here * [packages][m]: mv @flowershow/remark-wiki-link here * [packages][m]: mv @flowershow/remark-embed here * [remark-callouts/project.json][xs]: adjst test pattern * [package.json][s]: install missing deps * [remark-callouts][xs]: adjst fields in package.json * [remark-callouts][s]: rm pubish targets and adjst build executor * [remark-embed/jest.config][xs]: rm unknown option coverageDirectory * [remark-embed][xs]: rm publish targets * [remark-embed][s]: rename to @portaljs/remark-embed * [remark-wiki-link/eslintrc][xs]:adjst ignorePatterns * [package.json][xs]: install missing deps * [remark-wiki-link/test][xs]:specify format - also temporarily force any type on htmlExtension * [remark-wiki-link/README][xs]: replace @flowershow with @portaljs * [remark-wiki-link][xs]:rm old changelog * [remark-wiki-link][xs]: adjst package.json * [remark-wiki-link/project.json][xs]: rm publish targets * [core][s]: rm old changelog * [core/README][xs]:correct scope name * [remark-callouts/README][xs]: add @portaljs to pckg name * [remark-embed/README][xs]: add @portaljs to pckg name * [package-lock.json][xs]: refresh after rebasing on main
169 lines
4.1 KiB
TypeScript
169 lines
4.1 KiB
TypeScript
// Adjusted copy of https://github.com/landakram/micromark-extension-wiki-link/blob/master/src/index.js
|
|
import { codes } from "micromark-util-symbol/codes.js";
|
|
|
|
export interface SyntaxOptions {
|
|
aliasDivider?: string;
|
|
}
|
|
|
|
function isEndOfLineOrFile(code: number) {
|
|
return (
|
|
code === codes.carriageReturnLineFeed ||
|
|
code === codes.carriageReturn ||
|
|
code === codes.lineFeed ||
|
|
code === codes.eof
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Token types:
|
|
* - `wikiLink`:
|
|
* - `wikiLinkMarker`: The opening and closing brackets
|
|
* - `wikiLinkData`: The data between the brackets
|
|
* - `wikiLinkTarget`: The target of the link (the part before the alias divider)
|
|
* - `wikiLinkAliasMarker`: The alias divider
|
|
* - `wikiLinkAlias`: The alias of the link (the part after the alias divider)
|
|
* */
|
|
|
|
function wikiLink(opts: SyntaxOptions = {}) {
|
|
const aliasDivider = opts.aliasDivider || "|";
|
|
|
|
const aliasMarker = aliasDivider.charCodeAt(0);
|
|
const startMarker = codes.leftSquareBracket;
|
|
const embedStartMarker = codes.exclamationMark;
|
|
const endMarker = codes.rightSquareBracket;
|
|
|
|
function tokenize(effects, ok, nok) {
|
|
let data = false;
|
|
let alias = false;
|
|
|
|
let startMarkerCount = 0;
|
|
let endMarkerCount = 0;
|
|
|
|
return start;
|
|
|
|
// recognize the start of a wiki link
|
|
function start(code: number) {
|
|
if (code === startMarker) {
|
|
effects.enter("wikiLink");
|
|
effects.enter("wikiLinkMarker");
|
|
|
|
return consumeStart(code);
|
|
} else if (code === embedStartMarker) {
|
|
effects.enter("wikiLink", { isType: "embed" });
|
|
effects.enter("wikiLinkMarker", { isType: "embed" });
|
|
|
|
return consumeStart(code);
|
|
} else {
|
|
return nok(code);
|
|
}
|
|
}
|
|
function consumeStart(code: number) {
|
|
// when coursor is at the first character after the start marker `[[`
|
|
if (startMarkerCount === 2) {
|
|
effects.exit("wikiLinkMarker");
|
|
return consumeData(code);
|
|
}
|
|
|
|
if (code === startMarker || code === embedStartMarker) {
|
|
if (code === startMarker) {
|
|
startMarkerCount++;
|
|
}
|
|
effects.consume(code);
|
|
return consumeStart;
|
|
} else {
|
|
return nok(code);
|
|
}
|
|
}
|
|
|
|
function consumeData(code: number) {
|
|
if (isEndOfLineOrFile(code)) {
|
|
return nok(code);
|
|
}
|
|
|
|
effects.enter("wikiLinkData");
|
|
effects.enter("wikiLinkTarget");
|
|
return consumeTarget(code);
|
|
}
|
|
|
|
function consumeTarget(code: number) {
|
|
if (code === aliasMarker) {
|
|
if (!data) return nok(code);
|
|
effects.exit("wikiLinkTarget");
|
|
effects.enter("wikiLinkAliasMarker");
|
|
return consumeAliasMarker(code);
|
|
}
|
|
|
|
if (code === endMarker) {
|
|
if (!data) return nok(code);
|
|
effects.exit("wikiLinkTarget");
|
|
effects.exit("wikiLinkData");
|
|
effects.enter("wikiLinkMarker");
|
|
return consumeEnd(code);
|
|
}
|
|
|
|
if (isEndOfLineOrFile(code)) {
|
|
return nok(code);
|
|
}
|
|
|
|
data = true;
|
|
effects.consume(code);
|
|
|
|
return consumeTarget;
|
|
}
|
|
|
|
function consumeAliasMarker(code) {
|
|
effects.consume(code);
|
|
effects.exit("wikiLinkAliasMarker");
|
|
effects.enter("wikiLinkAlias");
|
|
return consumeAlias(code);
|
|
}
|
|
|
|
function consumeAlias(code: number) {
|
|
if (code === endMarker) {
|
|
if (!alias) return nok(code);
|
|
effects.exit("wikiLinkAlias");
|
|
effects.exit("wikiLinkData");
|
|
effects.enter("wikiLinkMarker");
|
|
return consumeEnd(code);
|
|
}
|
|
|
|
if (isEndOfLineOrFile(code)) {
|
|
return nok(code);
|
|
}
|
|
|
|
alias = true;
|
|
effects.consume(code);
|
|
|
|
return consumeAlias;
|
|
}
|
|
|
|
function consumeEnd(code: number) {
|
|
if (endMarkerCount === 2) {
|
|
effects.exit("wikiLinkMarker");
|
|
effects.exit("wikiLink");
|
|
return ok(code);
|
|
}
|
|
|
|
if (code !== endMarker) {
|
|
return nok(code);
|
|
}
|
|
|
|
effects.consume(code);
|
|
endMarkerCount++;
|
|
|
|
return consumeEnd;
|
|
}
|
|
}
|
|
|
|
const wikiLinkConstruct = { tokenize };
|
|
|
|
return {
|
|
text: {
|
|
[codes.leftSquareBracket]: wikiLinkConstruct,
|
|
[codes.exclamationMark]: wikiLinkConstruct,
|
|
},
|
|
};
|
|
}
|
|
|
|
export { wikiLink as syntax };
|