Skip to content

Commit

Permalink
Title
Browse files Browse the repository at this point in the history
Collecting and rendering titles to Pretext
  • Loading branch information
Bo-Y-G committed Aug 15, 2024
1 parent b557e23 commit 1932d04
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
28 changes: 28 additions & 0 deletions packages/unified-latex-to-pretext/libs/title.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as Ast from "@unified-latex/unified-latex-types";
import { visit } from "@unified-latex/unified-latex-util-visit";
import { match } from "@unified-latex/unified-latex-util-match";
import { htmlLike } from "@unified-latex/unified-latex-util-html-like";

export function gatherTitle(ast: Ast.Ast): Ast.Node[] {
const ti: Ast.Node[] = [];

visit(ast, (node) => {
if (match.macro(node, "title") && node.args) {
const titleContent = Object.fromEntries(
node.args.map((x) => [x.content])
);
return titleContent;
}
});
return ti;
}

export function renderTitle(ast: Ast.Ast, title: Ast.Node): Ast.Macro {

const renderedAuthorList = htmlLike({
tag: "title",
content: title,
});

return renderedAuthorList;
}
41 changes: 41 additions & 0 deletions packages/unified-latex-to-pretext/tests/title.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { describe, it, expect } from "vitest";
import Prettier from "prettier";
import util from "util";
import { getParser } from "@unified-latex/unified-latex-util-parse";
import { toXml } from "xast-util-to-xml";
import { xmlCompilePlugin } from "../libs/convert-to-pretext";
import { unified } from "unified";
import { gatherTitle, renderTitle } from "../libs/title";
import { toPretextWithLoggerFactory } from "../libs/pretext-subs/to-pretext";

function normalizeHtml(str: string) {
try {
return Prettier.format(str, { parser: "html" });
} catch {
console.warn("Could not format HTML string", str);
return str;
}
}

// Make console.log pretty-print by default
const origLog = console.log;
console.log = (...args) => {
origLog(...args.map((x) => util.inspect(x, false, 10, true)));
};

describe("unified-latex-to-pretext:title", () => {
let sample: string;
let input: string;
const parser = getParser();

it("collects title content", () => {
sample =
"\\title{Title}";
expect(gatherTitle(parser.parse(sample))).toEqual([]);

sample =
"\\title{Title} \n \\maketitle";
input = " Title"
expect(gatherTitle(parser.parse(sample))).toEqual(parser.parse(input).content);

Check failure on line 39 in packages/unified-latex-to-pretext/tests/title.test.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

packages/unified-latex-to-pretext/tests/title.test.ts > unified-latex-to-pretext:title > collects title content

AssertionError: expected [] to deeply equal [ { type: 'string', …(2) } ] - Expected + Received - Array [ - Object { - "content": "Title", - "position": Object { - "end": Object { - "column": 11, - "line": 1, - "offset": 10, - }, - "source": undefined, - "start": Object { - "column": 6, - "line": 1, - "offset": 5, - }, - }, - "type": "string", - }, - ] + Array [] ❯ packages/unified-latex-to-pretext/tests/title.test.ts:39:51
});
});

0 comments on commit 1932d04

Please sign in to comment.