Skip to content

Commit

Permalink
fix tests and types
Browse files Browse the repository at this point in the history
  • Loading branch information
dmca-glasgow committed Oct 29, 2024
1 parent 7d52ea4 commit 5ac0ee1
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 35 deletions.
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/processor/src/__test__/lists.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ test('parsing bug3', async () => {

const expectedHtml = unindentStringAndTrim(`
<div id="lemma-1" class="boxout lemma">
<p><span class="type">Lemma <span class="count">1.</span></span> If $f: U$ then</p>
<p><span class="type">Lemma <span class="count">1.</span></span> If <code class="language-math math-inline">f: U</code> then</p>
<ul>
<li>
<p>(Additivity) Alpha</p>
Expand Down
23 changes: 15 additions & 8 deletions packages/processor/src/__test__/maths.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ test('parsing bug', async () => {

const expectedHtml = unindentStringAndTrim(`
<div id="example-1" class="boxout example">
<p><span class="type">Example <span class="count">1.</span></span> If $n$ is a natural number, then</p>$$\\dfrac{d}{dz}z^{n} = nz^{n-1}.$$<p>and bla bla bla.</p>
<p><span class="type">Example <span class="count">1.</span></span> If <code class="language-math math-inline">n</code> is a natural number, then</p><pre><code class="language-math math-display">\\dfrac{d}{dz}z^{n} = nz^{n-1}.</code></pre>
<p>and bla bla bla.</p>
</div>
<p>If $n$ is a natural number, then</p>
$$\\dfrac{d}{dz}z^{n} = nz^{n-1}.$$
<p>If <code class="language-math math-inline">n</code> is a natural number, then</p>
<pre><code class="language-math math-display">\\dfrac{d}{dz}z^{n} = nz^{n-1}.</code></pre>
<p>and bla bla bla.</p>
`);

Expand Down Expand Up @@ -105,10 +106,11 @@ test('parsing bug2', async () => {

const expectedHtml = unindentStringAndTrim(`
<div id="example-1" class="boxout example">
<p><span class="type">Example <span class="count">1.</span></span> If $n$ is a natural number, then</p>$$\\begin{align*}\\dfrac{d}{dz}z^{n} = nz^{n-1}.\\end{align*}$$<p>and bla bla bla.</p>
<p><span class="type">Example <span class="count">1.</span></span> If <code class="language-math math-inline">n</code> is a natural number, then</p><pre><code class="language-math math-display">\\begin{align*}\\dfrac{d}{dz}z^{n} = nz^{n-1}.\\end{align*}</code></pre>
<p>and bla bla bla.</p>
</div>
<p>If $n$ is a natural number, then</p>
$$\\begin{align*}\\dfrac{d}{dz}z^{n} = nz^{n-1}.\\end{align*}$$
<p>If <code class="language-math math-inline">n</code> is a natural number, then</p>
<pre><code class="language-math math-display">\\begin{align*}\\dfrac{d}{dz}z^{n} = nz^{n-1}.\\end{align*}</code></pre>
<p>and bla bla bla.</p>
`);

Expand Down Expand Up @@ -186,7 +188,12 @@ test('parsing bug3', async () => {

const expectedHtml = unindentStringAndTrim(`
<div id="solution-1" class="boxout solution">
<p><span class="type">Solution.</span> We treat the equation</p>$$\\tfrac{1}{2}(e^{iz}+ e^{-iz}) = 10$$<p>as a quadratic equation in $e^{iz}$. After rearrangement it becomes</p>$$(e^{iz})^{2}-20 e^{iz}+ 1 = 0.$$<p>The solutions are</p>$$e^{iz}= \\dfrac{20\\pm\\sqrt{396}}{2}= 10\\pm\\sqrt{99},$$<p>so that</p>$$iz = \\log(10\\pm\\sqrt{99}) + 2k\\pi i,$$<p>that is,</p>$$z = -i\\log(10\\pm\\sqrt{99}) + 2k\\pi,$$<p>where $k$ is an arbitrary integer.<span class="proof-box">◻</span></p>
<p><span class="type">Solution.</span> We treat the equation</p><pre><code class="language-math math-display">\\tfrac{1}{2}(e^{iz}+ e^{-iz}) = 10</code></pre>
<p>as a quadratic equation in <code class="language-math math-inline">e^{iz}</code>. After rearrangement it becomes</p><pre><code class="language-math math-display">(e^{iz})^{2}-20 e^{iz}+ 1 = 0.</code></pre>
<p>The solutions are</p><pre><code class="language-math math-display">e^{iz}= \\dfrac{20\\pm\\sqrt{396}}{2}= 10\\pm\\sqrt{99},</code></pre>
<p>so that</p><pre><code class="language-math math-display">iz = \\log(10\\pm\\sqrt{99}) + 2k\\pi i,</code></pre>
<p>that is,</p><pre><code class="language-math math-display">z = -i\\log(10\\pm\\sqrt{99}) + 2k\\pi,</code></pre>
<p>where <code class="language-math math-inline">k</code> is an arbitrary integer.<span class="proof-box">◻</span></p>
</div>
`);

Expand Down Expand Up @@ -214,7 +221,7 @@ test('parsing bug4', async () => {

const expectedHtml = unindentStringAndTrim(`
<div class="framed">
<p>The $A$.</p>
<p>The <code class="language-math math-inline">A</code>.</p>
</div>
`);

Expand Down
2 changes: 1 addition & 1 deletion packages/processor/src/__test__/references.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test('environment references', async () => {

const expected = unindentStringAndTrim(`
<div id="cmd11" class="boxout definition">
<p><span class="type">Definition <span class="count">1.</span></span> Let $\\epsilon > 0$ be a subset.</p>
<p><span class="type">Definition <span class="count">1.</span></span> Let <code class="language-math math-inline">\\epsilon > 0</code> be a subset.</p>
</div>
<p>Definition <a href="#cmd11" class="ref">1</a> is automatically satisfied.</p>
`);
Expand Down
2 changes: 1 addition & 1 deletion packages/processor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export { inputToMarkdown } from './latex-to-markdown';

export {
markdownToJs,
runOptions,
createRunOptions,
sidebarRunOptions,
} from './markdown-to-mdx';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function createRehypePlugins(ctx: Context, options: Options) {
export async function toHast(
children: PhrasingContent[],
ctx: Context,
options?: Partial<Options>,
options: Partial<Options> = {},
) {
const processor = unified().use(
createRehypeFragmentPlugins(ctx, options),
Expand All @@ -42,7 +42,7 @@ export async function toHast(

function createRehypeFragmentPlugins(
_ctx: Context,
options: Partial<Options> = {},
_options: Partial<Options> = {},
): PluggableList {
return [
// TODO:
Expand Down
28 changes: 16 additions & 12 deletions packages/processor/src/markdown-to-mdx/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createRehypePlugins, processorOptions } from './hast-transforms';
import { markdownToMdast } from './mdast-transforms';
import { useMDXComponents } from './mdx-handlers';
import { createMDXComponents } from './mdx-handlers';
import { Options, defaultOptions } from './options';
import { RunOptions, createProcessor } from '@mdx-js/mdx';
import { Fragment, jsx, jsxDEV, jsxs } from 'preact/jsx-runtime';
Expand All @@ -18,17 +18,21 @@ export { MathsProvider, MathsContext } from './mdx-handlers/math';

export { sidebarRunOptions } from './sidebar';

export const runOptions: RunOptions = {
Fragment,
useMDXComponents,

// @ts-expect-error: jsx is incompatible for unknown reasons
jsx,
// @ts-expect-error: jsxs is incompatible for unknown reasons
jsxs,
// @ts-expect-error: jsxDEV is incompatible for unknown reasons
jsxDEV,
};
export function createRunOptions(
options: Partial<Options> = {},
): RunOptions {
return {
Fragment,
useMDXComponents: createMDXComponents(options),

// @ts-expect-error: jsx is incompatible for unknown reasons
jsx,
// @ts-expect-error: jsxs is incompatible for unknown reasons
jsxs,
// @ts-expect-error: jsxDEV is incompatible for unknown reasons
jsxDEV,
};
}

export async function markdownToJs(
markdown: string,
Expand Down
12 changes: 8 additions & 4 deletions packages/processor/src/markdown-to-mdx/mdx-handlers/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Options } from '../options';
import { MDXComponents } from 'mdx/types';

import { MathJax } from './math';
import { Task } from './task/Task';
import { Section } from './toc-highlight/section';

export function useMDXComponents(): MDXComponents {
export function createMDXComponents(options: Partial<Options>) {
// const ctx = {}
return {
return (): MDXComponents => ({
div(props) {
if (props.class?.includes('task')) {
return <Task {...props} />;
Expand All @@ -18,10 +19,13 @@ export function useMDXComponents(): MDXComponents {
// TODO: remove sectionize and use the titles
section: Section,
code(props) {
if (props.class?.includes('language-math')) {
if (
props.class?.includes('language-math') &&
options.mathsAsTex === false
) {
return <MathJax expr={props.children} />;
}
return <code {...props} />;
},
};
});
}
14 changes: 10 additions & 4 deletions packages/processor/src/test-utils/unit-test-processor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { FileType, inputToMarkdown, markdownToJs, runOptions } from '..';
import {
FileType,
createRunOptions,
inputToMarkdown,
markdownToJs,
} from '..';
import { run } from '@mdx-js/mdx';
import { resolve } from 'pathe';
import { createElement } from 'preact';
Expand Down Expand Up @@ -27,11 +32,12 @@ async function markdownToHtml(md: string) {
const prepared = unindentStringAndTrim(md);
const ctx = createTestContext(FileType.markdown, prepared);
const markdown = await inputToMarkdown(ctx);
const { article } = await markdownToJs(markdown, {
const options = {
mathsAsTex: true,
noWrapper: true,
});
const component = await run(article, runOptions);
};
const { article } = await markdownToJs(markdown, options);
const component = await run(article, createRunOptions(options));
// @ts-expect-error
const element = createElement(component.default);
return formatHtml(renderToString(element));
Expand Down
1 change: 1 addition & 0 deletions packages/runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@babel/preset-typescript": "^7.24.1",
"@linaria/core": "^6.1.0",
"@linaria/react": "^6.1.0",
"@types/prismjs": "^1.26.5",
"@wyw-in-js/babel-preset": "^0.5.1",
"@wyw-in-js/vite": "^0.5.1",
"vite-plugin-prismjs": "^0.0.11"
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime/src/article.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { MDXModule } from 'mdx/types';
import { useEffect, useState } from 'preact/hooks';
import { Fragment } from 'preact/jsx-runtime';

import { runOptions } from '@isos/processor';
import { createRunOptions } from '@isos/processor';

type Props = {
jsString: string;
Expand All @@ -15,7 +15,7 @@ export function Article({ jsString }: Props) {

useEffect(() => {
(async () => {
setMDX(await run(jsString, runOptions));
setMDX(await run(jsString, createRunOptions()));

// window.MathJax = {
// tex: {
Expand Down

0 comments on commit 5ac0ee1

Please sign in to comment.