This commit is contained in:
parent
21b857b457
commit
8d391a2e5e
42 changed files with 4590 additions and 40 deletions
101
sites/reflections/src/remark/callouts.mjs
Normal file
101
sites/reflections/src/remark/callouts.mjs
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
const CALLOUT_LABELS = new Map([
|
||||
["aside", "Aside"],
|
||||
["caution", "Caution"],
|
||||
["note", "Note"],
|
||||
["tip", "Tip"],
|
||||
["warning", "Warning"],
|
||||
]);
|
||||
|
||||
function textFromInline(node) {
|
||||
if (!node) return "";
|
||||
if (typeof node.value === "string") return node.value;
|
||||
if (!Array.isArray(node.children)) return "";
|
||||
return node.children.map(textFromInline).join("");
|
||||
}
|
||||
|
||||
function normalizeLabel(value) {
|
||||
return value
|
||||
.trim()
|
||||
.replace(/^["“”]+|["“”]+$/g, "")
|
||||
.replace(/:$/, "")
|
||||
.toLowerCase();
|
||||
}
|
||||
|
||||
function paragraphCalloutLabel(paragraph) {
|
||||
if (paragraph?.type !== "paragraph") return null;
|
||||
|
||||
const first = paragraph.children?.[0];
|
||||
if (first?.type === "text") {
|
||||
const match = first.value.match(/^\s*\[!(aside|caution|note|tip|warning)\]\s*/i);
|
||||
if (match) {
|
||||
return CALLOUT_LABELS.get(normalizeLabel(match[1]));
|
||||
}
|
||||
}
|
||||
|
||||
if (first?.type !== "strong") return null;
|
||||
|
||||
const label = CALLOUT_LABELS.get(normalizeLabel(textFromInline(first)));
|
||||
if (!label) return null;
|
||||
|
||||
return label;
|
||||
}
|
||||
|
||||
function removeLeadingLabel(paragraph, label) {
|
||||
if (paragraph?.type !== "paragraph") return;
|
||||
|
||||
const first = paragraph.children?.[0];
|
||||
if (first?.type === "text") {
|
||||
first.value = first.value.replace(/^\s*\[!(aside|caution|note|tip|warning)\]\s*/i, "");
|
||||
if (!first.value) paragraph.children.shift();
|
||||
return;
|
||||
}
|
||||
|
||||
const firstText = normalizeLabel(textFromInline(first));
|
||||
if (!first || first.type !== "strong" || firstText !== label.toLowerCase()) {
|
||||
return;
|
||||
}
|
||||
|
||||
paragraph.children.shift();
|
||||
|
||||
const next = paragraph.children[0];
|
||||
if (next?.type === "text") {
|
||||
next.value = next.value.replace(/^:\s*/, "").replace(/^\s+/, "");
|
||||
if (!next.value) paragraph.children.shift();
|
||||
}
|
||||
}
|
||||
|
||||
function transformBlockquote(node) {
|
||||
if (node.type !== "blockquote") return;
|
||||
|
||||
const first = node.children?.[0];
|
||||
const label = paragraphCalloutLabel(first);
|
||||
if (!label) return;
|
||||
|
||||
removeLeadingLabel(first, label);
|
||||
if (first.children?.length === 0) {
|
||||
node.children.shift();
|
||||
}
|
||||
|
||||
node.data = {
|
||||
hName: "aside",
|
||||
hProperties: {
|
||||
className: ["callout", `callout-${label.toLowerCase()}`],
|
||||
dataCalloutLabel: label,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function visit(node) {
|
||||
transformBlockquote(node);
|
||||
|
||||
if (!Array.isArray(node.children)) return;
|
||||
for (const child of node.children) {
|
||||
visit(child);
|
||||
}
|
||||
}
|
||||
|
||||
export default function remarkCallouts() {
|
||||
return function transformer(tree) {
|
||||
visit(tree);
|
||||
};
|
||||
}
|
||||
29
sites/reflections/src/remark/code-fence-languages.mjs
Normal file
29
sites/reflections/src/remark/code-fence-languages.mjs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
const LANGUAGE_ALIASES = new Map([["ojs", "js"]]);
|
||||
|
||||
const normalizeLanguage = (language) => {
|
||||
if (!language) return language;
|
||||
|
||||
const match = language.match(/^\{(.+)\}$/);
|
||||
if (!match) return language;
|
||||
|
||||
const normalized = match[1].trim().toLowerCase();
|
||||
return LANGUAGE_ALIASES.get(normalized) ?? normalized;
|
||||
};
|
||||
|
||||
const visit = (node) => {
|
||||
if (node.type === "code") {
|
||||
node.lang = normalizeLanguage(node.lang);
|
||||
}
|
||||
|
||||
if (!Array.isArray(node.children)) return;
|
||||
|
||||
for (const child of node.children) {
|
||||
visit(child);
|
||||
}
|
||||
};
|
||||
|
||||
const remarkCodeFenceLanguages = () => (tree) => {
|
||||
visit(tree);
|
||||
};
|
||||
|
||||
export default remarkCodeFenceLanguages;
|
||||
289
sites/reflections/src/remark/inline-footnotes.mjs
Normal file
289
sites/reflections/src/remark/inline-footnotes.mjs
Normal file
|
|
@ -0,0 +1,289 @@
|
|||
import { fromMarkdown } from "mdast-util-from-markdown";
|
||||
|
||||
const INLINE_FOOTNOTE_PREFIX = "inline-footnote";
|
||||
|
||||
const findInlineFootnote = (value, fromIndex = 0) => {
|
||||
const start = value.indexOf("^[", fromIndex);
|
||||
|
||||
if (start === -1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let depth = 1;
|
||||
|
||||
for (let index = start + 2; index < value.length; index += 1) {
|
||||
const character = value[index];
|
||||
|
||||
if (character === "\\" && index + 1 < value.length) {
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (character === "[") {
|
||||
depth += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (character === "]") {
|
||||
depth -= 1;
|
||||
|
||||
if (depth === 0) {
|
||||
return {
|
||||
start,
|
||||
end: index + 1,
|
||||
value: value.slice(start + 2, index).trim(),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
const createFootnoteChildren = (value) => {
|
||||
const tree = fromMarkdown(value);
|
||||
|
||||
if (tree.children.length > 0) {
|
||||
return tree.children;
|
||||
}
|
||||
|
||||
return [
|
||||
{
|
||||
type: "paragraph",
|
||||
children: [
|
||||
{
|
||||
type: "text",
|
||||
value,
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
};
|
||||
|
||||
const createFootnoteParagraph = (children) => ({
|
||||
type: "paragraph",
|
||||
children:
|
||||
children.length > 0
|
||||
? children
|
||||
: [
|
||||
{
|
||||
type: "text",
|
||||
value: "",
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const createFootnoteDefinition = (identifier, label, value) => ({
|
||||
type: "footnoteDefinition",
|
||||
identifier,
|
||||
label,
|
||||
children: createFootnoteChildren(value),
|
||||
});
|
||||
|
||||
const createFootnoteDefinitionFromChildren = (identifier, label, children) => ({
|
||||
type: "footnoteDefinition",
|
||||
identifier,
|
||||
label,
|
||||
children: [createFootnoteParagraph(children)],
|
||||
});
|
||||
|
||||
const createFootnoteReference = (context) => {
|
||||
context.count += 1;
|
||||
|
||||
const label = String(context.count);
|
||||
const identifier = `${INLINE_FOOTNOTE_PREFIX}-${label}`;
|
||||
|
||||
return {
|
||||
identifier,
|
||||
label,
|
||||
node: {
|
||||
type: "footnoteReference",
|
||||
identifier,
|
||||
label,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
const findClosingBracket = (value) => {
|
||||
for (let index = 0; index < value.length; index += 1) {
|
||||
const character = value[index];
|
||||
|
||||
if (character === "\\" && index + 1 < value.length) {
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (character === "]") {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
};
|
||||
|
||||
const cloneNode = (node) => JSON.parse(JSON.stringify(node));
|
||||
|
||||
const splitTextNode = (node, context) => {
|
||||
const replacements = [];
|
||||
let cursor = 0;
|
||||
let match = findInlineFootnote(node.value, cursor);
|
||||
|
||||
while (match) {
|
||||
if (match.start > cursor) {
|
||||
replacements.push({
|
||||
type: "text",
|
||||
value: node.value.slice(cursor, match.start),
|
||||
});
|
||||
}
|
||||
|
||||
if (match.value) {
|
||||
const reference = createFootnoteReference(context);
|
||||
replacements.push(reference.node);
|
||||
|
||||
context.definitions.push(
|
||||
createFootnoteDefinition(reference.identifier, reference.label, match.value),
|
||||
);
|
||||
} else {
|
||||
replacements.push({
|
||||
type: "text",
|
||||
value: node.value.slice(match.start, match.end),
|
||||
});
|
||||
}
|
||||
|
||||
cursor = match.end;
|
||||
match = findInlineFootnote(node.value, cursor);
|
||||
}
|
||||
|
||||
if (cursor < node.value.length) {
|
||||
replacements.push({
|
||||
type: "text",
|
||||
value: node.value.slice(cursor),
|
||||
});
|
||||
}
|
||||
|
||||
return replacements;
|
||||
};
|
||||
|
||||
const splitAcrossInlineNodes = (children, startIndex, context) => {
|
||||
const startNode = children[startIndex];
|
||||
const start = startNode.value.indexOf("^[");
|
||||
|
||||
if (start === -1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (findInlineFootnote(startNode.value, start)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const footnoteChildren = [];
|
||||
const replacements = [];
|
||||
const before = startNode.value.slice(0, start);
|
||||
const initialText = startNode.value.slice(start + 2);
|
||||
|
||||
if (before) {
|
||||
replacements.push({
|
||||
type: "text",
|
||||
value: before,
|
||||
});
|
||||
}
|
||||
|
||||
if (initialText) {
|
||||
footnoteChildren.push({
|
||||
type: "text",
|
||||
value: initialText,
|
||||
});
|
||||
}
|
||||
|
||||
for (let index = startIndex + 1; index < children.length; index += 1) {
|
||||
const child = children[index];
|
||||
|
||||
if (child.type === "text") {
|
||||
const closingIndex = findClosingBracket(child.value);
|
||||
|
||||
if (closingIndex !== -1) {
|
||||
const footnoteText = child.value.slice(0, closingIndex);
|
||||
const after = child.value.slice(closingIndex + 1);
|
||||
|
||||
if (footnoteText) {
|
||||
footnoteChildren.push({
|
||||
type: "text",
|
||||
value: footnoteText,
|
||||
});
|
||||
}
|
||||
|
||||
const reference = createFootnoteReference(context);
|
||||
replacements.push(reference.node);
|
||||
|
||||
if (after) {
|
||||
replacements.push({
|
||||
type: "text",
|
||||
value: after,
|
||||
});
|
||||
}
|
||||
|
||||
context.definitions.push(
|
||||
createFootnoteDefinitionFromChildren(
|
||||
reference.identifier,
|
||||
reference.label,
|
||||
footnoteChildren,
|
||||
),
|
||||
);
|
||||
|
||||
return {
|
||||
consumed: index - startIndex + 1,
|
||||
replacements,
|
||||
};
|
||||
}
|
||||
|
||||
footnoteChildren.push(cloneNode(child));
|
||||
continue;
|
||||
}
|
||||
|
||||
footnoteChildren.push(cloneNode(child));
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
const visitChildren = (node, context) => {
|
||||
if (!Array.isArray(node.children)) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (let index = 0; index < node.children.length; index += 1) {
|
||||
const child = node.children[index];
|
||||
|
||||
if (child.type === "text" && child.value.includes("^[")) {
|
||||
const acrossNodes = splitAcrossInlineNodes(node.children, index, context);
|
||||
|
||||
if (acrossNodes) {
|
||||
node.children.splice(index, acrossNodes.consumed, ...acrossNodes.replacements);
|
||||
index += acrossNodes.replacements.length - 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
const replacements = splitTextNode(child, context);
|
||||
node.children.splice(index, 1, ...replacements);
|
||||
index += replacements.length - 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
visitChildren(child, context);
|
||||
}
|
||||
};
|
||||
|
||||
const remarkInlineFootnotes = () => (tree) => {
|
||||
const context = {
|
||||
count: 0,
|
||||
definitions: [],
|
||||
};
|
||||
|
||||
visitChildren(tree, context);
|
||||
|
||||
if (context.definitions.length > 0) {
|
||||
tree.children.push(...context.definitions);
|
||||
}
|
||||
};
|
||||
|
||||
export default remarkInlineFootnotes;
|
||||
Loading…
Add table
Add a link
Reference in a new issue