Skip to content

Commit

Permalink
Replace \paragraph and \subparagraph in HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
siefkenj committed Sep 30, 2023
1 parent fbf9c16 commit 431e936
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,18 @@ function factory(
};
}

function createHeading(tag: string) {
function createHeading(tag: string, attrs = {}) {
return (macro: Ast.Macro) => {
const args = getArgsContent(macro);
const starred = !!args[0];
const attributes: Record<string, string> = starred
? { className: "starred" }
: {};

if (attrs) {
Object.assign(attributes, attrs);
}

return htmlLike({
tag,
content: args[args.length - 1] || [],
Expand All @@ -59,6 +64,8 @@ export const macroReplacements: Record<string, (node: Ast.Macro) => Ast.Node> =
section: createHeading("h3"),
subsection: createHeading("h4"),
subsubsection: createHeading("h5"),
paragraph: createHeading("h6", { className: "section-paragraph" }),
subparagraph: createHeading("h6", { className: "section-subparagraph" }),
appendix: createHeading("h2"),
smallskip: () =>
htmlLike({
Expand Down
2 changes: 2 additions & 0 deletions packages/unified-latex-to-hast/libs/wrap-pars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export function wrapPars(
"section",
"subsection",
"subsubsection",
"paragraph",
"subparagraph",
"vspace",
"smallskip",
"medskip",
Expand Down
1 change: 1 addition & 0 deletions packages/unified-latex-to-hast/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
},
"scripts": {
"build": "npm run clean && mkdirp ./dist && npm run compile",
"test": "vitest",
"clean": "rm -rf ./dist && rm -rf tsconfig.tsbuildinfo",
"compile": "tsc -b tsconfig.json & node build.js & wait",
"package": "node ../../scripts/make-package.mjs",
Expand Down
11 changes: 11 additions & 0 deletions packages/unified-latex-to-hast/tests/unified-latex-to-hast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,15 @@ describe("unified-latex-to-hast:unified-latex-to-hast", () => {
</ol>`)
);
});
it("replaces paragraphs", () => {
let ast;

ast = process(`\\paragraph{Important.} Paragraph`);
expect(normalizeHtml(ast)).toEqual(
normalizeHtml(`
<h6 class="section-paragraph">Important.</h6>
Paragraph
`)
);
});
});

0 comments on commit 431e936

Please sign in to comment.