Skip to content

Commit

Permalink
Support \ref and related commands (#131)
Browse files Browse the repository at this point in the history
* support \ref and related xrefs; plus \latex, etc.
  • Loading branch information
oscarlevin authored Feb 11, 2025
1 parent d00877a commit ef2cd8c
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,47 @@ export const macroReplacements: Record<
content: args[1] || [],
});
},
ref: (node) => {
const args = getArgsContent(node);
const ref = printRaw(args[1] || "");
return htmlLike({
tag: "xref",
attributes: {
ref: ref || "",
text: "global",
},
});
},
cref: (node) => {
const args = getArgsContent(node);
const ref = printRaw(args[1] || "");
return htmlLike({
tag: "xref",
attributes: {
ref: ref || "",
},
});
},
Cref: (node) => {
const args = getArgsContent(node);
const ref = printRaw(args[1] || "");
return htmlLike({
tag: "xref",
attributes: {
ref: ref || "",
},
});
},
cite: (node) => {
const args = getArgsContent(node);
const ref = printRaw(args[1] || "");
return htmlLike({
tag: "xref",
attributes: {
ref: ref || "",
},
});
},
"\\": emptyStringWithWarningFactory(
`Warning: There is no equivalent tag for \"\\\", an empty Ast.String was used as a replacement.`
),
Expand All @@ -150,6 +191,19 @@ export const macroReplacements: Record<
noindent: emptyStringWithWarningFactory(
`Warning: There is no equivalent tag for \"noindent\", an empty Ast.String was used as a replacement.`
),
latex: (node) => {
return htmlLike({tag: "latex"})
},
latexe: (node) => {
return htmlLike({tag: "latex"})
},
today: (node) => {
return htmlLike({tag: "today"})
},
tex: (node) => {
return htmlLike({tag: "tex"})
},
//tex: factory("tex"),
includegraphics: (node) => {
const args = getArgsContent(node);
const source = printRaw(args[args.length - 1] || []);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,4 +387,31 @@ describe("unified-latex-to-pretext:unified-latex-to-pretext", () => {
await normalizeHtml(`<remark><title>My remark</title><p>a</p></remark>`)
);
});
it("Replaces \\ref with a xref", async () => {
html = process(`Exercise \\ref{foo} is important`);
expect(await normalizeHtml(html)).toEqual(
await normalizeHtml(`Exercise <xref ref="foo" text="global"/> is important`)
);
});
it("Replaces \\cref and \\Cref with a bare xref", async () => {
html = process(`As we saw in \\cref{foo}, we can do this.`);
expect(await normalizeHtml(html)).toEqual(
await normalizeHtml(`As we saw in <xref ref="foo"/>, we can do this.`)
);

html = process(`As we saw in \\Cref{foo}, we can do this.`);
expect(await normalizeHtml(html)).toEqual(
await normalizeHtml(`As we saw in <xref ref="foo" />, we can do this.`)
);
});
it("Replaces \\cite with a xref", async () => {
html = process(`See \\cite{foo} for more`);
expect(await normalizeHtml(html)).toEqual(
await normalizeHtml(`See <xref ref="foo" /> for more`)
);
});
it("Replaces \\latex with <latex/> etc.", async () => {
html = process(`We can write in \\latex or \\tex and do so \\today.`);
expect(await normalizeHtml(html)).toEqual(await normalizeHtml(`We can write in <latex/> or <tex/> and do so <today/>.`));
})
});

0 comments on commit ef2cd8c

Please sign in to comment.