Skip to content

Commit 3a37b0c

Browse files
committed
misc bugfixes and styling
1 parent dd96a30 commit 3a37b0c

File tree

9 files changed

+319
-123
lines changed

9 files changed

+319
-123
lines changed

packages/processor/src/__test__/lists.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,39 @@ test('description to dl', async () => {
131131

132132
expect(html).toBe(expectedHtml);
133133
});
134+
135+
test.skip('description with newlines to dl', async () => {
136+
const markdown = await testProcessor.latex(String.raw`
137+
\begin{description}
138+
\item[Nabla identities] $a$,\\
139+
$b$,\\
140+
$c$.
141+
\end{description}
142+
`);
143+
// console.log(markdown);
144+
// return;
145+
146+
const expectedMarkdown = unindentStringAndTrim(String.raw`
147+
Nabla identities
148+
: $a$,\
149+
$b$,\
150+
$c$.
151+
`);
152+
153+
expect(markdown).toBe(expectedMarkdown);
154+
155+
const html = await testProcessor.md(markdown);
156+
// console.log(html);
157+
158+
const expectedHtml = unindentStringAndTrim(`
159+
<dl>
160+
<dt>Nabla identities</dt>
161+
<dd><code class="latex">a</code>,<br />
162+
<code class="latex">b</code>,<br />
163+
<code class="latex">c</code>.
164+
</dd>
165+
</dl>
166+
`);
167+
168+
expect(html).toBe(expectedHtml);
169+
});

packages/processor/src/markdown-to-mdx/hast-transforms/index.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,6 @@ export function createRehypePlugins(
4747
return plugins;
4848
}
4949

50-
// export async function toHast(
51-
// children: PhrasingContent[],
52-
// ctx: Context,
53-
// options: Partial<Options> = {},
54-
// ) {
55-
// const processor = unified().use(
56-
// createRehypeFragmentPlugins(ctx, options),
57-
// );
58-
59-
// const root: Root = {
60-
// type: 'root',
61-
// children,
62-
// };
63-
// const hast = (await processor.run(root)) as Root;
64-
// return hast.children as ElementContent[];
65-
// }
66-
6750
function createRehypeFragmentPlugins(
6851
ctx: Context,
6952
_options: Partial<Options> = {},
@@ -72,7 +55,6 @@ function createRehypeFragmentPlugins(
7255
addDefaultAltText,
7356
missingMathsImageToSvg,
7457
addMathsRefsAndCount,
75-
removeEmptyParagraphs,
7658

7759
[replaceFootnoteRefDefs, ctx],
7860
[footNotesToSideNotes, ctx],
@@ -84,6 +66,7 @@ function createRehypeFragmentPlugins(
8466
// properties: { className: 'link' },
8567
// },
8668
// ],
69+
removeEmptyParagraphs,
8770

8871
// should be last
8972
[mathTagToRefLabel, ctx],

packages/processor/src/plugins/div-syntax/mdx-divs.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ export function divSyntax(_ctx: Context) {
2525
visit(tree, 'containerDirective', (node) => {
2626
if (node.name === ' ') {
2727
const id = node.attributes?.id;
28+
const className = node.attributes?.class;
29+
2830
if (id) {
2931
const [abbr] = id.split('-');
3032
const float = defaultFloats.find((o) => o.abbr === abbr);
@@ -34,11 +36,8 @@ export function divSyntax(_ctx: Context) {
3436
createFigure(node, float.name, ctxObj, id);
3537
}
3638
}
37-
}
38-
39-
const klass = node.attributes?.class;
40-
if (klass) {
41-
const classes = klass.split(' ');
39+
} else if (className) {
40+
const classes = className.split(' ');
4241
if (classes.includes('fig')) {
4342
const float = defaultFloats.find((o) => o.abbr === 'fig');
4443
if (float) {
@@ -75,7 +74,7 @@ function createFigure(
7574

7675
const contentHast = getContentHast(content);
7776

78-
children.push(...contentHast);
77+
children.push(contentHast);
7978

8079
// if (caption.length > 0) {
8180
const strong: Element = {
@@ -191,7 +190,16 @@ function getContentHast(content: (BlockContent | DefinitionContent)[]) {
191190
children: content,
192191
}) as Element;
193192

194-
return contentHast.children;
193+
const elem: ElementContent = {
194+
type: 'element',
195+
tagName: 'div',
196+
properties: {
197+
className: ['fig-content'],
198+
},
199+
children: contentHast.children,
200+
};
201+
202+
return elem;
195203
}
196204

197205
function removeDupes(arr: string[]) {

packages/processor/src/plugins/images/__test__/images.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,3 +638,50 @@ test('images with no label or caption', async () => {
638638
639639
expect(html).toBe(expectedHtml);
640640
});
641+
642+
test('images with includegraphics*', async () => {
643+
const latex = String.raw`
644+
\documentclass{article}
645+
\usepackage{graphicx}
646+
\begin{document}
647+
648+
\begin{figure}
649+
\includegraphics*[alt={Alpha}]{fig/ex1-2a.png}
650+
\includegraphics*[alt={Bravo}]{fig/ex1-2b.png}
651+
\includegraphics*[alt={Charlie}]{fig/ex1-2c.png}
652+
\end{figure}
653+
654+
\end{document}
655+
`;
656+
657+
const markdown = await testProcessor.latex(latex);
658+
// console.log(markdown);
659+
660+
const expectedMarkdown = unindentStringAndTrim(`
661+
::: {.fig}
662+
663+
![](fig/ex1-2a.png){alt="Alpha"}
664+
665+
![](fig/ex1-2b.png){alt="Bravo"}
666+
667+
![](fig/ex1-2c.png){alt="Charlie"}
668+
669+
:::
670+
`);
671+
672+
expect(markdown).toBe(expectedMarkdown);
673+
674+
const html = await testProcessor.md(markdown);
675+
// console.log(html);
676+
677+
const expectedHtml = unindentStringAndTrim(String.raw`
678+
<figure>
679+
<p><img src="fig/ex1-2a.png" alt="Alpha" /></p>
680+
<p><img src="fig/ex1-2b.png" alt="Bravo" /></p>
681+
<p><img src="fig/ex1-2c.png" alt="Charlie" /></p>
682+
<figcaption><strong>Figure 1</strong></figcaption>
683+
</figure>
684+
`);
685+
686+
expect(html).toBe(expectedHtml);
687+
});

packages/processor/src/plugins/images/create-image.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,19 @@ export function createImage(node: Macro): Macro {
2323
const attrs = args.slice(0, -1).filter(Boolean).flat() as Ast.Node[];
2424
// console.dir(attrs, { depth: null });
2525

26+
const altIdx = attrs.findIndex((o) => {
27+
return o.type === 'string' && o.content === 'alt';
28+
});
2629
if (
27-
attrs[0] &&
28-
attrs[0].type === 'string' &&
29-
attrs[0].content === 'alt' &&
30-
attrs[2].type === 'group'
30+
altIdx !== -1 &&
31+
attrs.length > 2 &&
32+
attrs[altIdx + 1].type === 'string' &&
33+
// @ts-expect-error
34+
attrs[altIdx + 1].content === '=' &&
35+
attrs[altIdx + 2].type === 'group'
3136
) {
32-
attributes.alt = convertToMarkdown(attrs[2].content).trim();
37+
// @ts-expect-error
38+
attributes.alt = convertToMarkdown(attrs[altIdx + 2].content).trim();
3339
}
3440

3541
// const id = attrs.find(

packages/processor/src/plugins/images/pandoc-implicit-figures.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,30 @@ export function pandocImplicitFigures() {
9696
);
9797
}
9898

99+
const figContent: Element = {
100+
type: 'element',
101+
tagName: 'div',
102+
properties: {
103+
className: ['fig-content'],
104+
},
105+
children: [
106+
{
107+
type: 'element',
108+
tagName: 'p',
109+
properties: {},
110+
children: [img],
111+
},
112+
],
113+
};
114+
99115
parent.data = {
100116
hName: 'figure',
101117
hProperties: {
102118
src: null,
103119
alt: null,
104120
id,
105121
},
106-
hChildren: [img, figCaption],
122+
hChildren: [figContent, figCaption],
107123
};
108124
});
109125
// console.dir(tree, { depth: null });

packages/processor/src/plugins/maths/__test__/maths.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,50 @@ test('expand newcommand inside newcommand', async () => {
359359
// console.log(quartoHtml);
360360
});
361361

362+
test('maths equation with tag', async () => {
363+
const latex = String.raw`
364+
\documentclass{article}
365+
\usepackage{amsmath}
366+
\begin{document}
367+
\begin{equation}
368+
a+b\tag{$*$}
369+
\end{equation}
370+
\end{document}
371+
`;
372+
373+
const markdown = await testProcessor.latex(latex);
374+
// console.log(markdown);
375+
// return;
376+
377+
const expectedMarkdown = unindentStringAndTrim(String.raw`
378+
$$
379+
\begin{equation}a+b\tag{$*$}\end{equation}
380+
$$
381+
`);
382+
383+
expect(markdown).toBe(expectedMarkdown);
384+
385+
const html = await testProcessor.md(markdown, {
386+
// state: {
387+
// maths: {
388+
// mathsAsTex: false,
389+
// mathsFontName: 'computerModern',
390+
// syntaxHighlight: false,
391+
// },
392+
// },
393+
});
394+
// console.log(html);
395+
396+
const expectedHtml = unindentStringAndTrim(String.raw`
397+
<p class="maths env-equation"><code class="latex">\begin{equation}a+b\end{equation}</code><span class="eq-tag">(<code class="latex">*</code>)</span></p>
398+
`);
399+
400+
expect(html).toBe(expectedHtml);
401+
402+
// const quartoHtml = await markdownToQuartoHtml(markdown);
403+
// console.log(quartoHtml);
404+
});
405+
362406
test('maths equations with labels and tags', async () => {
363407
const latex = String.raw`
364408
\documentclass{article}

packages/processor/src/plugins/maths/math-tag-to-ref-label.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { Root } from 'hast';
1+
import { ElementContent, Parent, Root } from 'hast';
2+
import remarkRehype from 'remark-rehype';
23
import { visit } from 'unist-util-visit';
34

5+
import { createRemarkProcessor } from '../../remark-processor';
6+
47
const pattern = /\\tag\{(.+?)\}/;
58

69
export function mathTagToRefLabel() {
710
return (tree: Root) => {
8-
// console.dir(tree, { depth: null });
911
visit(tree, 'element', (node) => {
1012
if (node.tagName === 'pre') {
1113
if (node.children.length > 1) {
@@ -28,13 +30,18 @@ export function mathTagToRefLabel() {
2830
if (match !== null) {
2931
// remove tag
3032
codeFirst.value = codeFirst.value.replace(pattern, '');
33+
3134
// add tag as eq-count attribute
35+
const tag = match[1];
3236
const eqCountFirst = eqCount.children[0];
33-
if (
34-
eqCountFirst.type === 'element' &&
35-
eqCountFirst.properties['data-id']
36-
) {
37-
eqCountFirst.properties['data-tag'] = match[1];
37+
if (eqCountFirst.type === 'element') {
38+
const id = eqCountFirst.properties['data-id'];
39+
if (id) {
40+
eqCountFirst.properties['data-tag'] = tag;
41+
} else {
42+
eqCount.properties.className = ['eq-tag'];
43+
eqCount.children = getTagHast(`(${tag})`);
44+
}
3845
}
3946
}
4047
}
@@ -45,3 +52,16 @@ export function mathTagToRefLabel() {
4552
});
4653
};
4754
}
55+
56+
const processor = createRemarkProcessor([remarkRehype]);
57+
58+
function getTagHast(tag: string) {
59+
const parsed = processor.parse(String(tag));
60+
const transformed = processor.runSync(parsed) as Parent;
61+
62+
if (transformed.children.length === 0) {
63+
return [];
64+
}
65+
const firstChild = transformed.children[0] as Parent;
66+
return firstChild.children as ElementContent[];
67+
}

0 commit comments

Comments
 (0)