thibaud frere commited on
Commit Β·
2903163
1
Parent(s): f43302a
update references
Browse filesFiles changed (9)
- app/.astro/astro/content.d.ts-238
- app/astro.config.mjs+1-1
- app/scripts/latex-to-markdown/filters/equation-ids.luaAdded+116
- app/scripts/latex-to-markdown/latex-converter.mjs+8-1
- app/scripts/latex-to-markdown/output/main.md
- app/scripts/latex-to-markdown/output/main.mdx
- app/scripts/latex-to-markdown/post-processor.mjs+61
- app/scripts/latex-to-markdown/reference-preprocessor.mjsAdded+171
- app/src/content/article.mdx
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238declare module 'astro:content' {interface Render {'.mdx': Promise<{Content: import('astro').MarkdownInstance<{}>['Content'];headings: import('astro').MarkdownHeading[];remarkPluginFrontmatter: Record<string, any>;components: import('astro').MDXInstance<{}>['components'];}>;}}declare module 'astro:content' {interface RenderResult {Content: import('astro/runtime/server/index.js').AstroComponentFactory;headings: import('astro').MarkdownHeading[];remarkPluginFrontmatter: Record<string, any>;}interface Render {'.md': Promise<RenderResult>;}export interface RenderedContent {html: string;metadata?: {imagePaths: Array<string>;[key: string]: unknown;};}}declare module 'astro:content' {type Flatten<T> = T extends { [K: string]: infer U } ? U : never;export type CollectionKey = keyof AnyEntryMap;export type CollectionEntry<C extends CollectionKey> = Flatten<AnyEntryMap[C]>;export type ContentCollectionKey = keyof ContentEntryMap;export type DataCollectionKey = keyof DataEntryMap;type AllValuesOf<T> = T extends any ? T[keyof T] : never;type ValidContentEntrySlug<C extends keyof ContentEntryMap> = AllValuesOf<ContentEntryMap[C]>['slug'];/** @deprecated Use `getEntry` instead. */export function getEntryBySlug<C extends keyof ContentEntryMap,E extends ValidContentEntrySlug<C> | (string & {}),>(collection: C,// Note that this has to accept a regular string too, for SSRentrySlug: E,): E extends ValidContentEntrySlug<C>? Promise<CollectionEntry<C>>: Promise<CollectionEntry<C> | undefined>;/** @deprecated Use `getEntry` instead. */export function getDataEntryById<C extends keyof DataEntryMap, E extends keyof DataEntryMap[C]>(collection: C,entryId: E,): Promise<CollectionEntry<C>>;export function getCollection<C extends keyof AnyEntryMap, E extends CollectionEntry<C>>(collection: C,filter?: (entry: CollectionEntry<C>) => entry is E,): Promise<E[]>;export function getCollection<C extends keyof AnyEntryMap>(collection: C,filter?: (entry: CollectionEntry<C>) => unknown,): Promise<CollectionEntry<C>[]>;export function getEntry<C extends keyof ContentEntryMap,E extends ValidContentEntrySlug<C> | (string & {}),>(entry: {collection: C;slug: E;}): E extends ValidContentEntrySlug<C>? Promise<CollectionEntry<C>>: Promise<CollectionEntry<C> | undefined>;export function getEntry<C extends keyof DataEntryMap,E extends keyof DataEntryMap[C] | (string & {}),>(entry: {collection: C;id: E;}): E extends keyof DataEntryMap[C]? Promise<DataEntryMap[C][E]>: Promise<CollectionEntry<C> | undefined>;export function getEntry<C extends keyof ContentEntryMap,E extends ValidContentEntrySlug<C> | (string & {}),>(collection: C,slug: E,): E extends ValidContentEntrySlug<C>? Promise<CollectionEntry<C>>: Promise<CollectionEntry<C> | undefined>;export function getEntry<C extends keyof DataEntryMap,E extends keyof DataEntryMap[C] | (string & {}),>(collection: C,id: E,): E extends keyof DataEntryMap[C]? Promise<DataEntryMap[C][E]>: Promise<CollectionEntry<C> | undefined>;/** Resolve an array of entry references from the same collection */export function getEntries<C extends keyof ContentEntryMap>(entries: {collection: C;slug: ValidContentEntrySlug<C>;}[],): Promise<CollectionEntry<C>[]>;export function getEntries<C extends keyof DataEntryMap>(entries: {collection: C;id: keyof DataEntryMap[C];}[],): Promise<CollectionEntry<C>[]>;export function render<C extends keyof AnyEntryMap>(entry: AnyEntryMap[C][string],): Promise<RenderResult>;export function reference<C extends keyof AnyEntryMap>(collection: C,): import('astro/zod').ZodEffects<import('astro/zod').ZodString,C extends keyof ContentEntryMap? {collection: C;slug: ValidContentEntrySlug<C>;}: {collection: C;id: keyof DataEntryMap[C];}>;// Allow generic `string` to avoid excessive type errors in the config// if `dev` is not running to update as you edit.// Invalid collection names will be caught at build time.export function reference<C extends string>(collection: C,): import('astro/zod').ZodEffects<import('astro/zod').ZodString, never>;type ReturnTypeOrOriginal<T> = T extends (...args: any[]) => infer R ? R : T;type InferEntrySchema<C extends keyof AnyEntryMap> = import('astro/zod').infer<ReturnTypeOrOriginal<Required<ContentConfig['collections'][C]>['schema']>>;type ContentEntryMap = {"chapters": {"00_abstract.mdx": {id: "00_abstract.mdx";slug: "00_abstract";body: string;collection: "chapters";data: any} & { render(): Render[".mdx"] };"01_introduction.mdx": {id: "01_introduction.mdx";slug: "01_introduction";body: string;collection: "chapters";data: any} & { render(): Render[".mdx"] };"02_classic_robotics.mdx": {id: "02_classic_robotics.mdx";slug: "02_classic_robotics";body: string;collection: "chapters";data: any} & { render(): Render[".mdx"] };"03_reinforcement_learning.mdx": {id: "03_reinforcement_learning.mdx";slug: "03_reinforcement_learning";body: string;collection: "chapters";data: any} & { render(): Render[".mdx"] };"04_imitation_learning.mdx": {id: "04_imitation_learning.mdx";slug: "04_imitation_learning";body: string;collection: "chapters";data: any} & { render(): Render[".mdx"] };"06_next_directions.mdx": {id: "06_next_directions.mdx";slug: "06_next_directions";body: string;collection: "chapters";data: any} & { render(): Render[".mdx"] };"07_conclusions.mdx": {id: "07_conclusions.mdx";slug: "07_conclusions";body: string;collection: "chapters";data: any} & { render(): Render[".mdx"] };"A_foreword.mdx": {id: "A_foreword.mdx";slug: "a_foreword";body: string;collection: "chapters";data: any} & { render(): Render[".mdx"] };};"test": {"converted-sample.mdx": {id: "converted-sample.mdx";slug: "converted-sample";body: string;collection: "test";data: any} & { render(): Render[".mdx"] };};};type DataEntryMap = {"assets": {"data/somedata": {id: "data/somedata";collection: "assets";data: any};};};type AnyEntryMap = ContentEntryMap & DataEntryMap;export type ContentConfig = never;}
55 unmodified lines5657585960616255 unmodified linesrehypePlugins: [rehypeSlug,[rehypeAutolinkHeadings, { behavior: 'wrap' }],rehypeKatex,[rehypeCitation, {bibliography: 'src/content/bibliography.bib',linkCitations: true,55 unmodified lines5657585960616255 unmodified linesrehypePlugins: [rehypeSlug,[rehypeAutolinkHeadings, { behavior: 'wrap' }],[rehypeKatex, { trust: true }],[rehypeCitation, {bibliography: 'src/content/bibliography.bib',linkCitations: true,
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116--[[Pandoc Lua filter to add IDs to equations using KaTeX \htmlId syntax
This filter processes display math equations and inline math that contain \label{} commands, and wraps them with \htmlId{clean-id}{content} for KaTeX.
Requirements:- KaTeX renderer with trust: true option- Equations with \label{} commands in LaTeX--]]
-- Function to clean identifier strings (remove prefixes and colons)function clean_identifier(id_str) if id_str and type(id_str) == "string" then -- Remove common prefixes and replace colons with dashes local clean = id_str :gsub("^(eq|equation):", "") -- Remove eq: prefix :gsub(":", "-") -- Replace colons with dashes :gsub("[^a-zA-Z0-9_-]", "-") -- Replace other problematic chars :gsub("-+", "-") -- Collapse multiple dashes :gsub("^-", "") -- Remove leading dash :gsub("-$", "") -- Remove trailing dash -- Ensure we don't have empty identifiers if clean == "" then clean = id_str:gsub(":", "-") end return clean end return id_strend
-- Process Math elements (both inline and display)function Math(el) local math_content = el.text -- Look for \label{...} commands in the math content local label_match = math_content:match("\\label%{([^}]+)%}") if label_match then -- Clean the identifier local clean_id = clean_identifier(label_match) -- Remove the \label{} command from the math content local clean_math = math_content:gsub("\\label%{[^}]+%}", "") -- Clean up any extra whitespace or line breaks that might remain clean_math = clean_math:gsub("%s*$", ""):gsub("^%s*", "") -- Remove problematic equation environments that don't work well with \htmlId clean_math = clean_math:gsub("\\begin%{equation%}", ""):gsub("\\end%{equation%}", "") clean_math = clean_math:gsub("\\begin%{align%}", ""):gsub("\\end%{align%}", "") clean_math = clean_math:gsub("\\begin%{equation%*%}", ""):gsub("\\end%{equation%*%}", "") clean_math = clean_math:gsub("\\begin%{align%*%}", ""):gsub("\\end%{align%*%}", "") -- Clean up any remaining whitespace clean_math = clean_math:gsub("%s*$", ""):gsub("^%s*", "") -- Wrap the equation content with \htmlId{} local new_math = "\\htmlId{" .. clean_id .. "}{" .. clean_math .. "}" -- Return new Math element with the updated content return pandoc.Math(el.mathtype, new_math) end -- Return unchanged if no label found return elend
-- Optional: Process RawInline elements that might contain LaTeX mathfunction RawInline(el) if el.format == "latex" or el.format == "tex" then local content = el.text -- Look for equation environments with labels local label_match = content:match("\\label%{([^}]+)%}") if label_match then local clean_id = clean_identifier(label_match) -- For raw LaTeX, we might need different handling -- This is a simplified approach - adjust based on your needs local clean_content = content:gsub("\\label%{[^}]+%}", "") if clean_content:match("\\begin%{equation") or clean_content:match("\\begin%{align") then -- For equation environments, we might need to wrap differently -- This depends on how your KaTeX setup handles equation environments return pandoc.RawInline(el.format, clean_content) end end end return elend
-- Optional: Process RawBlock elements for display equationsfunction RawBlock(el) if el.format == "latex" or el.format == "tex" then local content = el.text -- Look for equation environments with labels local label_match = content:match("\\label%{([^}]+)%}") if label_match then local clean_id = clean_identifier(label_match) local clean_content = content:gsub("\\label%{[^}]+%}", "") -- For block equations, we might want to preserve the structure -- but add the htmlId functionality return pandoc.RawBlock(el.format, clean_content) end end return elend5 unmodified lines67891011155 unmodified lines16716816917017117253 unmodified lines2262272282292302312325 unmodified linesimport { fileURLToPath } from 'url';import { cleanBibliography } from './bib-cleaner.mjs';import { postProcessMarkdown } from './post-processor.mjs';const __filename = fileURLToPath(import.meta.url);const __dirname = dirname(__filename);155 unmodified lines}}// Write the preprocessed filewriteFileSync(tempFile, content);return tempFile;53 unmodified linesconst mediaDir = join(outputDir, 'assets', 'image');ensureDirectory(mediaDir);const inputDir = dirname(inputFile);const pandocCommand = `pandoc "${preprocessedFile}" -f latex+latex_macros -t gfm+tex_math_dollars --shift-heading-level-by=1 --wrap=none ${bibOption} --extract-media="${mediaDir}" --resource-path="${inputDir}" -o "${outputFile}"`;console.log(` Running: ${pandocCommand}`);execSync(pandocCommand, { stdio: 'pipe' });5 unmodified lines6789101112155 unmodified lines16816917017117217317417517617717853 unmodified lines2322332342352362372382395 unmodified linesimport { fileURLToPath } from 'url';import { cleanBibliography } from './bib-cleaner.mjs';import { postProcessMarkdown } from './post-processor.mjs';import { preprocessLatexReferences } from './reference-preprocessor.mjs';const __filename = fileURLToPath(import.meta.url);const __dirname = dirname(__filename);155 unmodified lines}}// Apply reference preprocessing AFTER input inclusion to ensure all references are capturedconsole.log('π§ Preprocessing LaTeX references for MDX compatibility...');const referenceResult = preprocessLatexReferences(content);content = referenceResult.content;// Write the preprocessed filewriteFileSync(tempFile, content);return tempFile;53 unmodified linesconst mediaDir = join(outputDir, 'assets', 'image');ensureDirectory(mediaDir);const inputDir = dirname(inputFile);const equationFilterPath = join(__dirname, 'filters', 'equation-ids.lua');const pandocCommand = `pandoc "${preprocessedFile}" -f latex+latex_macros -t gfm+tex_math_dollars --shift-heading-level-by=1 --wrap=none ${bibOption} --extract-media="${mediaDir}" --resource-path="${inputDir}" --lua-filter="${equationFilterPath}" -o "${outputFile}"`;console.log(` Running: ${pandocCommand}`);execSync(pandocCommand, { stdio: 'pipe' });
196 unmodified lines19719819920020120212 unmodified lines215216217218219220196 unmodified linesreturn processedContent;}/*** Main post-processing function that applies all cleanup steps* @param {string} content - Raw Markdown content from Pandoc12 unmodified linesprocessedContent = fixMathCommands(processedContent);processedContent = fixUnicodeIssues(processedContent);processedContent = fixMultilineMath(processedContent);// Inject code snippets if input directory is providedif (inputDir) {196 unmodified lines19719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026112 unmodified lines274275276277278279280281196 unmodified linesreturn processedContent;}/*** Fix all attributes that still contain colons (href, data-reference, id)* @param {string} content - Markdown content* @returns {string} - Cleaned content*/function fixAllAttributes(content) {console.log(' π Fixing all attributes with colons...');let fixedCount = 0;// Fix href attributes containing colonscontent = content.replace(/href="([^"]*):([^"]*)"/g, (match, before, after) => {fixedCount++;return `href="${before}-${after}"`;});// Fix data-reference attributes containing colonscontent = content.replace(/data-reference="([^"]*):([^"]*)"/g, (match, before, after) => {fixedCount++;return `data-reference="${before}-${after}"`;});// Fix id attributes containing colons (like in ResponsiveImage components)content = content.replace(/id="([^"]*):([^"]*)"/g, (match, before, after) => {fixedCount++;return `id="${before}-${after}"`;});if (fixedCount > 0) {console.log(` β Fixed ${fixedCount} attribute(s) with colons`);}return content;}/*** Fix link text content that still contains colons* @param {string} content - Markdown content* @returns {string} - Cleaned content*/function fixLinkTextContent(content) {console.log(' π Fixing link text content with colons...');let fixedCount = 0;// Fix text content within links that contain references with colons// Pattern: <a ...>[text:content]</a>const cleanedContent = content.replace(/<a([^>]*)>\[([^:]*):([^\]]*)\]<\/a>/g, (match, attributes, before, after) => {fixedCount++;return `<a${attributes}>[${before}-${after}]</a>`;});if (fixedCount > 0) {console.log(` β Fixed ${fixedCount} link text(s) with colons`);}return cleanedContent;}/*** Main post-processing function that applies all cleanup steps* @param {string} content - Raw Markdown content from Pandoc12 unmodified linesprocessedContent = fixMathCommands(processedContent);processedContent = fixUnicodeIssues(processedContent);processedContent = fixMultilineMath(processedContent);processedContent = fixAllAttributes(processedContent);processedContent = fixLinkTextContent(processedContent);// Inject code snippets if input directory is providedif (inputDir) {
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171#!/usr/bin/env node
/** * LaTeX Reference Preprocessor * * This module cleans up LaTeX references BEFORE Pandoc conversion to ensure * consistent, MDX-compatible identifiers throughout the document. * * What it does: * - Removes prefixes from labels: \label{sec:intro} β \label{sec-intro} * - Updates corresponding refs: \ref{sec:intro} β \ref{sec-intro} * - Handles all reference types: sec:, fig:, eq:, table:, etc. * - Maintains consistency between labels and references */
/** * Extract all references from LaTeX content * @param {string} content - LaTeX content * @returns {Object} - Object with labels and refs arrays */function extractReferences(content) { const references = { labels: new Set(), refs: new Set(), cites: new Set() };
// Find all \label{...} commands const labelMatches = content.matchAll(/\\label\{([^}]+)\}/g); for (const match of labelMatches) { references.labels.add(match[1]); }
// Find all \ref{...} commands const refMatches = content.matchAll(/\\ref\{([^}]+)\}/g); for (const match of refMatches) { references.refs.add(match[1]); }
// Find all \cite{...} commands (already handled in existing code but included for completeness) const citeMatches = content.matchAll(/\\cite[tp]?\{([^}]+)\}/g); for (const match of citeMatches) { // Handle multiple citations: \cite{ref1,ref2,ref3} const citations = match[1].split(',').map(cite => cite.trim()); citations.forEach(cite => references.cites.add(cite)); }
return references;}
/** * Create clean identifier mapping * @param {Object} references - References object from extractReferences * @returns {Map} - Mapping from original to clean identifiers */function createCleanMapping(references) { const mapping = new Map();
// Create mapping for all unique identifiers const allIdentifiers = new Set([ ...references.labels, ...references.refs ]);
for (const id of allIdentifiers) { // Remove common prefixes and replace colons with dashes let cleanId = id .replace(/^(sec|section|ch|chapter|fig|figure|eq|equation|tab|table|lst|listing|app|appendix):/gi, '') .replace(/:/g, '-') .replace(/[^a-zA-Z0-9_-]/g, '-') // Replace any other problematic characters .replace(/-+/g, '-') // Collapse multiple dashes .replace(/^-|-$/g, ''); // Remove leading/trailing dashes
// Ensure we don't have empty identifiers if (!cleanId) { cleanId = id.replace(/:/g, '-'); }
mapping.set(id, cleanId); }
return mapping;}
/** * Apply mapping to LaTeX content * @param {string} content - Original LaTeX content * @param {Map} mapping - Identifier mapping * @returns {string} - Cleaned LaTeX content */function applyMapping(content, mapping) { let cleanedContent = content; let changesCount = 0;
// Apply mapping to all identifiers for (const [original, clean] of mapping) { if (original !== clean) { // Replace \label{original} with \label{clean} const labelRegex = new RegExp(`\\\\label\\{${escapeRegex(original)}\\}`, 'g'); const labelMatches = cleanedContent.match(labelRegex); if (labelMatches) { cleanedContent = cleanedContent.replace(labelRegex, `\\label{${clean}}`); changesCount += labelMatches.length; }
// Replace \ref{original} with \ref{clean} const refRegex = new RegExp(`\\\\ref\\{${escapeRegex(original)}\\}`, 'g'); const refMatches = cleanedContent.match(refRegex); if (refMatches) { cleanedContent = cleanedContent.replace(refRegex, `\\ref{${clean}}`); changesCount += refMatches.length; } } }
return { content: cleanedContent, changesCount };}
/** * Escape special regex characters * @param {string} string - String to escape * @returns {string} - Escaped string */function escapeRegex(string) { return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');}
/** * Main preprocessing function * @param {string} latexContent - Original LaTeX content * @returns {Object} - Result with cleaned content and statistics */export function preprocessLatexReferences(latexContent) { console.log('π§ Preprocessing LaTeX references for MDX compatibility...');
// 1. Extract all references const references = extractReferences(latexContent);
console.log(` π Found: ${references.labels.size} labels, ${references.refs.size} refs`);
// 2. Create clean mapping const mapping = createCleanMapping(references);
// 3. Apply mapping const result = applyMapping(latexContent, mapping);
if (result.changesCount > 0) { console.log(` β
Cleaned ${result.changesCount} reference(s) for MDX compatibility`);
// Show some examples of changes let exampleCount = 0; for (const [original, clean] of mapping) { if (original !== clean && exampleCount < 3) { console.log(` ${original} β ${clean}`); exampleCount++; } } if (mapping.size > 3) { console.log(` ... and ${mapping.size - 3} more`); } } else { console.log(' βΉοΈ No reference cleanup needed'); }
return { content: result.content, changesCount: result.changesCount, mapping: mapping, references: references };}app/.astro/astro/content.d.ts-238
app/astro.config.mjs+1-1
app/scripts/latex-to-markdown/filters/equation-ids.luaAdded+116
app/scripts/latex-to-markdown/latex-converter.mjs+8-1
app/scripts/latex-to-markdown/output/main.md
app/scripts/latex-to-markdown/output/main.mdx
app/scripts/latex-to-markdown/post-processor.mjs+61
app/scripts/latex-to-markdown/reference-preprocessor.mjsAdded+171
app/src/content/article.mdx