Skip to content

Commit

Permalink
finish Maths2B tweaks 🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
dmca-glasgow committed Jul 11, 2024
1 parent 442e17b commit 6c4fe3c
Show file tree
Hide file tree
Showing 26 changed files with 415 additions and 256 deletions.
62 changes: 62 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: 'publish'

on:
push:
branches:
- main

# This workflow will trigger on each push to the `release` branch to create or update a GitHub release, build your app, and upload the artifacts to the release.

jobs:
publish-tauri:
permissions:
contents: write
strategy:
fail-fast: false
matrix:
include:
- platform: 'macos-latest' # for Arm based macs (M1 and above).
args: '--target aarch64-apple-darwin'
- platform: 'macos-latest' # for Intel based macs.
args: '--target x86_64-apple-darwin'
- platform: 'ubuntu-20.04' # for Tauri v1 you could replace this with ubuntu-20.04.
args: ''
- platform: 'windows-latest'
args: ''

runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v4

- name: setup node
uses: actions/setup-node@v4
with:
node-version: lts/*

- name: install Rust stable
uses: dtolnay/rust-toolchain@stable
with:
# Those targets are only used on macos runners so it's in an `if` to slightly speed up windows and linux builds.
targets: ${{ matrix.platform == 'macos-latest' && 'aarch64-apple-darwin,x86_64-apple-darwin' || '' }}

- name: install dependencies (ubuntu only)
if: matrix.platform == 'ubuntu-20.04' # This must match the platform value defined above.
run: |
sudo apt-get update
sudo apt-get install -y libwebkit2gtk-4.0-dev libappindicator3-dev librsvg2-dev patchelf
# webkitgtk 4.0 is for Tauri v1 - webkitgtk 4.1 is for Tauri v2.
# You can remove the one that doesn't apply to your app to speed up the workflow a bit.

- name: install frontend dependencies
run: yarn install # change this to npm, pnpm or bun depending on which one you use.

- uses: tauri-apps/tauri-action@v0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tagName: app-v__VERSION__ # the action automatically replaces \_\_VERSION\_\_ with the app version.
releaseName: 'App v__VERSION__'
releaseBody: 'See the assets to download this version and install.'
releaseDraft: true
prerelease: false
args: ${{ matrix.args }}
4 changes: 3 additions & 1 deletion packages/processor/fixtures/images/article.tex
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

\begin{document}

its reflection $\begin{bmatrix}a_1\\-a_2\end{bmatrix}$.
\begin{framed}
This operation caries the vector.
%
\begin{center}\includegraphics[scale=0.55]{./figs/Ex2-2-13reflection.png}\end{center}
%
Observe that
\end{framed}

\end{document}
28 changes: 8 additions & 20 deletions packages/processor/src/__test__/images.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,20 @@ import { expect, test } from 'vitest';
import { unindentStringAndTrim } from '../test-utils/unindent-string';
import { testProcessor } from '../test-utils/unit-test-processor';

test('parsing bug', async () => {
const markdown = await testProcessor.latex(`
\\begin{center}\\includegraphics[scale=0.55]{./figs/Ex2-2-13reflection.png}\\end{center}
`);
test('parsing bug 2', async () => {
const markdown = await testProcessor.fixture('images/article.tex');

const expectedMarkdown = unindentStringAndTrim(`
::::framed
This operation caries the vector.
:::center
![](./figs/Ex2-2-13reflection.png)
:::
Observe that
::::
`);

expect(markdown).toBe(expectedMarkdown);

// const html = await testProcessor.md(markdown);

// const expectedHtml = unindentStringAndTrim(`
// <div class="center">
// <p><img src="./figs/Ex2-2-13reflection.png" alt /></p>
// </div>
// `);

// expect(html).toBe(expectedHtml);
});

test.only('parsing bug 2', async () => {
const markdown = await testProcessor.fixture('images/article.tex');

console.log(markdown);
});
4 changes: 2 additions & 2 deletions packages/processor/src/__test__/primitives.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test('parsing bug', async () => {
*Example 2.1*
Example 2.1
:underline[Example 2.1]
`);

expect(markdown).toBe(expectedMarkdown);
Expand All @@ -36,7 +36,7 @@ test('parsing bug', async () => {
<p><strong>Example 2.1</strong></p>
<p><em>Example 2.1</em></p>
<p><em>Example 2.1</em></p>
<p>Example 2.1</p>
<p><u>Example 2.1</u></p>
`);

expect(html).toBe(expectedHtml);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ export function insertParbreaksAroundBlockElements() {
}

function shouldGetParbreaks(node: Node | Argument) {
return isDisplayMath(node) || isEnumerate(node) || isItemize(node);
return (
isDisplayMath(node) ||
isEnumerate(node) ||
isItemize(node) ||
isCenter(node)
);
}

function isDisplayMath(node: Node | Argument) {
Expand All @@ -55,6 +60,10 @@ function isItemize(node: Node | Argument) {
return node.type === 'environment' && node.env === 'itemize';
}

function isCenter(node: Node | Argument) {
return node.type === 'macro' && node.content === 'html-tag:center';
}

function isParBreak(node?: Node) {
return node?.type === 'parbreak';
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { createLabel } from './label';
import { createReference } from './reference';
import { createSideNote } from './sidenote';
import { createTitle } from './title';
import { createUnderline } from './underline';

// import { createUnderline } from './underline';

Expand Down Expand Up @@ -100,6 +101,12 @@ function spanHandler(ctx: Context, state: State, node: Element) {
state.patch(node, result);
return result;
}

if (className.includes('underline')) {
const result = createUnderline(state, node);
state.patch(node, result);
return result;
}
}

return state.all(node);
Expand Down Expand Up @@ -138,15 +145,25 @@ function divHandler(state: State, node: Element) {
return state.all(node);
}

function centerHandler(state: State, node: Element): ContainerDirective {
return {
type: 'containerDirective',
name: 'center',
children: [
{
type: 'paragraph',
children: state.all(node) as PhrasingContent[],
},
],
};
function centerHandler(state: State, node: Element) {
return [
{
type: 'text',
value: '\n\n',
},
{
type: 'containerDirective',
name: 'center',
children: [
{
type: 'paragraph',
children: state.all(node) as PhrasingContent[],
},
],
},
{
type: 'text',
value: '\n\n',
},
];
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import { Element } from 'hast';
import { State } from 'hast-util-to-mdast';
import { Text } from 'mdast';
import { TextDirective } from 'mdast-util-directive';

export function createUnderline(_state: State, node: Element): Text {
export function createUnderline(
_state: State,
node: Element
): TextDirective {
const text = node.children[0] as Text;
return {
type: 'text',
value: `<u>${text.value}</u>`,
type: 'textDirective',
name: 'underline',
children: [
{
type: 'text',
value: text.value,
},
],
};
}
76 changes: 56 additions & 20 deletions packages/processor/src/markdown-to-mdx/hast-transforms/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Options } from '../options';
import { ElementContent } from 'hast';
import { PhrasingContent, Root } from 'mdast';
// import { createSvg } from '../utils/icons';
// import autolinkHeadings from 'rehype-autolink-headings';
import mathjaxBrowser from 'rehype-mathjax/browser';
// import mathjaxChtml from 'rehype-mathjax/chtml';
import mathjaxSvg from 'rehype-mathjax/svg';
// import rehypeRaw from 'rehype-raw';
// import rehypeSlug from 'rehype-slug';
import { PluggableList } from 'unified';
import { PluggableList, unified } from 'unified';

// import { visit } from 'unist-util-visit';
import { Context } from '../context';
Expand All @@ -30,28 +32,62 @@ const mathjaxOptions = {
},
};

function createRehypeFragmentPlugins(
ctx: Context,
options: Partial<Options> = {}
) {
const plugins: PluggableList = [];

if (options.mathsAsTex) {
plugins.push([mathjaxBrowser, mathjaxOptions]);
} else {
plugins.push([mathjaxSvg, mathjaxOptions]);
}

return plugins;
}

export function createRehypePlugins(ctx: Context, options: Options) {
const plugins: PluggableList = [
[options.mathsAsTex ? mathjaxBrowser : mathjaxSvg, mathjaxOptions],
// () => (tree) => {
// console.dir(tree, { depth: null });
// },

// rehypeRaw,
// rehypeSlug,
// [
// autolinkHeadings,
// // {
// // content: createSvg('link-icon') as any,
// // properties: { className: 'link' },
// // },
// ],
// () => (tree) => {
// console.log(JSON.stringify(tree, null, 2));
// },
];
const plugins = createRehypeFragmentPlugins(ctx, options);

// plugins.push(
// () => (tree) => {
// console.dir(tree, { depth: null });
// },

// rehypeRaw,
// rehypeSlug,
// [
// autolinkHeadings,
// // {
// // content: createSvg('link-icon') as any,
// // properties: { className: 'link' },
// // },
// ],
// () => (tree) => {
// console.log(JSON.stringify(tree, null, 2));
// },
// );

if (!options.noWrapper) {
plugins.push([createWrapper, ctx]);
}
return plugins;
}

export async function toHast(
children: PhrasingContent[],
ctx: Context,
options?: Partial<Options>
) {
const processor = unified().use(
createRehypeFragmentPlugins(ctx, options)
);

const root: Root = {
type: 'root',
children,
};
const hast = (await processor.run(root)) as Root;
return hast.children as ElementContent[];
}
13 changes: 7 additions & 6 deletions packages/processor/src/markdown-to-mdx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,19 @@ const processorOptions: ProcessorOptions = {

export async function markdownToJs(
markdown: string,
options: Partial<Options> = {}
_options: Partial<Options> = {}
) {
const ctx = createContext();
const mdast = await markdownToMdast(markdown, ctx);
const options = {
...defaultOptions,
..._options,
};
const mdast = await markdownToMdast(markdown, ctx, options);
// console.dir(mdast, { depth: null });

const processor = createProcessor({
...processorOptions,
rehypePlugins: createRehypePlugins(ctx, {
...defaultOptions,
...options,
}),
rehypePlugins: createRehypePlugins(ctx, options),
});

// @ts-expect-error: mdast is not of type Program
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ import { Context } from '../context';
import { center } from './center';
import { extractFrontmatter } from './extract-frontmatter';
import { fancyTitle } from './fancy-title';
import { underline } from './underline';

export async function markdownToMdast(markdown: string, ctx: Context) {
export async function markdownToMdast(
markdown: string,
ctx: Context
// options: Options
) {
const processor = createRemarkProcessor([
[extractFrontmatter, ctx],
fancyTitle,
Expand All @@ -22,6 +27,7 @@ export async function markdownToMdast(markdown: string, ctx: Context) {
[boxouts, ctx],
center,
[sidenotes, ctx],
underline,

// should be last
escapeCharsForMdx,
Expand Down
Loading

0 comments on commit 6c4fe3c

Please sign in to comment.