Skip to content

Commit

Permalink
add maths as tex feature
Browse files Browse the repository at this point in the history
  • Loading branch information
dmca-glasgow committed Oct 29, 2024
1 parent d8e343f commit 7d52ea4
Show file tree
Hide file tree
Showing 19 changed files with 213 additions and 91 deletions.
39 changes: 37 additions & 2 deletions package-lock.json

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

Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@ import { ProcessorOptions } from '@mdx-js/mdx';
import { ElementContent } from 'hast';
import { PhrasingContent, Root } from 'mdast';
// import { createSvg } from '../utils/icons';
// import rehypeRaw from 'rehype-raw';
// import rehypeSlug from 'rehype-slug';
import { PluggableList, unified } from 'unified';

// import { visit } from 'unist-util-visit';
import { Context } from '../context';
// import { mathjax } from './mathjax';
import { createWrapper } from './wrapper';

export const processorOptions: ProcessorOptions = {
Expand Down Expand Up @@ -49,7 +45,6 @@ function createRehypeFragmentPlugins(
options: Partial<Options> = {},

Check failure on line 45 in packages/processor/src/markdown-to-mdx/hast-transforms/index.ts

View workflow job for this annotation

GitHub Actions / build-tauri (macos-latest, --target aarch64-apple-darwin)

'options' is declared but its value is never read.

Check failure on line 45 in packages/processor/src/markdown-to-mdx/hast-transforms/index.ts

View workflow job for this annotation

GitHub Actions / build-tauri (macos-latest, --target x86_64-apple-darwin)

'options' is declared but its value is never read.

Check failure on line 45 in packages/processor/src/markdown-to-mdx/hast-transforms/index.ts

View workflow job for this annotation

GitHub Actions / build-tauri (ubuntu-20.04)

'options' is declared but its value is never read.

Check failure on line 45 in packages/processor/src/markdown-to-mdx/hast-transforms/index.ts

View workflow job for this annotation

GitHub Actions / build-tauri (windows-latest)

'options' is declared but its value is never read.
): PluggableList {
return [
// mathjax(options),
// TODO:
// [
// autolinkHeadings,
Expand Down
31 changes: 0 additions & 31 deletions packages/processor/src/markdown-to-mdx/hast-transforms/mathjax.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/processor/src/markdown-to-mdx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export {
TocHighlightContext,
} from './mdx-handlers/toc-highlight/toc-highlight-provider';

export type { FontName } from './mdx-handlers/mathjax';
export type { FontName } from './mdx-handlers/math';

export { MathsProvider, MathsContext } from './mdx-handlers/mathjax';
export { MathsProvider, MathsContext } from './mdx-handlers/math';

export { sidebarRunOptions } from './sidebar';

Expand Down
7 changes: 2 additions & 5 deletions packages/processor/src/markdown-to-mdx/mdx-handlers/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MDXComponents } from 'mdx/types';

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

Expand All @@ -18,10 +18,7 @@ export function useMDXComponents(): MDXComponents {
// TODO: remove sectionize and use the titles
section: Section,
code(props) {
if (
props.class?.includes('math-inline') ||
props.class?.includes('math-display')
) {
if (props.class?.includes('language-math')) {
return <MathJax expr={props.children} />;
}
return <code {...props} />;
Expand Down
30 changes: 30 additions & 0 deletions packages/processor/src/markdown-to-mdx/mdx-handlers/math/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { useContext } from 'preact/hooks';

import { toLaTeX } from './latex';
import { getMathJax } from './mathjax';
import { MathsContext } from './maths-provider';

export { MathsContext, MathsProvider } from './maths-provider';

export type { FontName } from './mathjax';

type Props = {
expr: string;
};

export function MathJax({ expr }: Props) {
const ctx = useContext(MathsContext);
if (ctx.mathsAsTex) {
return (
<code
className="latex"
dangerouslySetInnerHTML={{
__html: toLaTeX(expr),
}}
/>
);
} else {
const children = getMathJax(expr, ctx.fontName);
return <>{children}</>;
}
}
13 changes: 13 additions & 0 deletions packages/processor/src/markdown-to-mdx/mdx-handlers/math/latex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Prism from 'prismjs';

Check failure on line 1 in packages/processor/src/markdown-to-mdx/mdx-handlers/math/latex.ts

View workflow job for this annotation

GitHub Actions / build-tauri (macos-latest, --target aarch64-apple-darwin)

Could not find a declaration file for module 'prismjs'. '/Users/runner/work/isos/isos/node_modules/prismjs/prism.js' implicitly has an 'any' type.

Check failure on line 1 in packages/processor/src/markdown-to-mdx/mdx-handlers/math/latex.ts

View workflow job for this annotation

GitHub Actions / build-tauri (macos-latest, --target x86_64-apple-darwin)

Could not find a declaration file for module 'prismjs'. '/Users/runner/work/isos/isos/node_modules/prismjs/prism.js' implicitly has an 'any' type.

Check failure on line 1 in packages/processor/src/markdown-to-mdx/mdx-handlers/math/latex.ts

View workflow job for this annotation

GitHub Actions / build-tauri (ubuntu-20.04)

Could not find a declaration file for module 'prismjs'. '/home/runner/work/isos/isos/node_modules/prismjs/prism.js' implicitly has an 'any' type.

Check failure on line 1 in packages/processor/src/markdown-to-mdx/mdx-handlers/math/latex.ts

View workflow job for this annotation

GitHub Actions / build-tauri (windows-latest)

Could not find a declaration file for module 'prismjs'. 'D:/a/isos/isos/node_modules/prismjs/prism.js' implicitly has an 'any' type.

export function toLaTeX(expr: string) {
const formatted = formatLaTeX(expr);
return Prism.highlight(formatted, Prism.languages.latex, 'latex');
}

function formatLaTeX(expr: string) {
return expr
.replace(/\\\\\s?/g, '\\\\\n')
.replace(/\\begin{align(\*?)}/g, '\\begin{align$1}\n')
.replace(/\\end{align(\*?)}/g, '\n\\end{align$1}');
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@ import { mathjax } from 'mathjax-full/js/mathjax.js';
import { SVG } from 'mathjax-full/js/output/svg.js';
import { MathJaxTermesFont } from 'mathjax-termes-font/js/svg.js';
import 'mathjax-termes-font/js/svg/dynamic/double-struck.js';
import { useContext } from 'preact/hooks';

import { render } from './litedom';
import { MathsContext } from './maths-provider';

export { MathsContext, MathsProvider } from './maths-provider';

type Document = MathDocument<LiteNode, LiteText, LiteDocument>;

Expand Down Expand Up @@ -61,23 +57,9 @@ const output: Output = {
}),
};

export type Options = {
fontName: FontName;
};

export function getMathjaxHtml(tex: string, options: Options) {
doc.outputJax = output[options.fontName];
export function getMathJax(expr: string, fontName: FontName) {
doc.outputJax = output[fontName];
doc.outputJax.setAdaptor(doc.adaptor);
return doc.convert(tex) as LiteElement;
}

type Props = {
expr: string;
};

export function MathJax({ expr }: Props) {
const ctx = useContext(MathsContext);
const node = getMathjaxHtml(expr, ctx);
const children = render(node.children);
return <>{children}</>;
const node = doc.convert(expr) as LiteElement;
return render(node.children);
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { ComponentChildren, createContext } from 'preact';
import { useMemo, useState } from 'preact/hooks';

import { FontName } from '@isos/processor';
import { FontName } from './mathjax';

const defaultFontName = 'termes';

type Maths = {
fontName: FontName;
mathsAsTex: boolean;
setFontName: (fontName: FontName) => unknown;
setMathsAsTex: (mathsAsTex: boolean) => unknown;
};

export const MathsContext = createContext<Maths>({
fontName: defaultFontName,
mathsAsTex: false,
setFontName: () => {},
setMathsAsTex: () => {},
});

export function MathsProvider({
Expand All @@ -21,13 +25,16 @@ export function MathsProvider({
children: ComponentChildren;
}) {
const [fontName, setFontName] = useState(defaultFontName);
const [mathsAsTex, setMathsAsTex] = useState(false);

const context = useMemo((): Maths => {
return {
fontName,
mathsAsTex,
setFontName,
setMathsAsTex,
};
}, [fontName]);
}, [fontName, mathsAsTex]);

return (
<MathsContext.Provider value={context}>
Expand Down
6 changes: 4 additions & 2 deletions packages/runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"@mdx-js/mdx": "^3.0.1",
"@mdx-js/preact": "^3.0.1",
"classnames": "^2.5.1",
"preact": "^10.20.2"
"preact": "^10.20.2",
"prismjs": "^1.29.0"
},
"devDependencies": {
"@babel/core": "^7.24.4",
Expand All @@ -28,6 +29,7 @@
"@linaria/core": "^6.1.0",
"@linaria/react": "^6.1.0",
"@wyw-in-js/babel-preset": "^0.5.1",
"@wyw-in-js/vite": "^0.5.1"
"@wyw-in-js/vite": "^0.5.1",
"vite-plugin-prismjs": "^0.0.11"
}
}
19 changes: 1 addition & 18 deletions packages/runtime/src/providers/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,12 @@ type Props = {
children: ComponentChildren;
};

// const mathjaxConfig: MathJax3Config = {
// tex: {
// inlineMath: [
// ['$', '$'],
// // ['\\(', '\\)'],
// ],
// displayMath: [
// ['$$', '$$'],
// // [`\\[`, `\\]`],
// ],
// },
// };

export function Providers({ children }: Props) {
return (
<TocHighlightProvider>
<LoadingProvider>
<ErrorProvider>
<MathsProvider>
{/* <MathJaxContext config={mathjaxConfig}> */}
{children}
{/* </MathJaxContext> */}
</MathsProvider>
<MathsProvider>{children}</MathsProvider>
</ErrorProvider>
</LoadingProvider>
</TocHighlightProvider>
Expand Down
1 change: 1 addition & 0 deletions packages/runtime/src/sidebar/checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const Wrapper = styled.div`
display: flex;
justify-content: space-between;
align-items: middle;
padding-bottom: 0.5em;
label {
font-size: 0.9em;
Expand Down
5 changes: 5 additions & 0 deletions packages/runtime/src/sidebar/view-options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ export function ViewOptions() {
maths.setFontName(sansSerif ? 'fira' : 'termes')
}
/>
<Checkbox
label="Maths as LaTeX"
value={maths.mathsAsTex}
onChange={(mathsAsTex) => maths.setMathsAsTex(mathsAsTex)}
/>
</Fieldset>
</form>
);
Expand Down
1 change: 1 addition & 0 deletions packages/runtime/src/styles/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ main {
@import './sidenote.scss';
@import './scrollbars.scss';
@import './range-input.scss';
@import './syntax-highlight.scss';

// @import './odl/index.scss';
// @import './latex/index.scss';
Expand Down
Loading

0 comments on commit 7d52ea4

Please sign in to comment.