Skip to content

Change to output code elements in HTML #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @typedef {import('./complex-types').InlineMath} InlineMath
*
* @typedef ToOptions
* Configuration to turn an AST into markdown.
* @property {boolean} [singleDollarTextMath=true]
* Whether to support math (text) with a single dollar (`boolean`, default:
* `true`).
Expand Down Expand Up @@ -46,9 +47,16 @@ export function mathFromMarkdown() {
meta: null,
value: '',
data: {
hName: 'div',
hProperties: {className: ['math', 'math-display']},
hChildren: [{type: 'text', value: ''}]
hName: 'pre',
hProperties: {},
hChildren: [
{
type: 'element',
tagName: 'code',
properties: {className: ['language-math', 'math-display']},
children: [{type: 'text', value: ''}]
}
]
}
},
token
Expand Down Expand Up @@ -81,7 +89,7 @@ export function mathFromMarkdown() {
const node = /** @type {Math} */ (this.exit(token))
node.value = data
// @ts-expect-error: we defined it.
node.data.hChildren[0].value = data
node.data.hChildren[0].children[0].value = data
this.setData('mathFlowInside')
}

Expand All @@ -92,8 +100,8 @@ export function mathFromMarkdown() {
type: 'inlineMath',
value: '',
data: {
hName: 'span',
hProperties: {className: ['math', 'math-inline']},
hName: 'code',
hProperties: {className: ['language-math', 'math-inline']},
hChildren: [{type: 'text', value: ''}]
}
},
Expand Down
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ When working with `mdast-util-from-markdown`, you must combine this package with
This utility adds [fields on nodes][fields] so that the utility responsible for
turning mdast (markdown) nodes into hast (HTML) nodes,
[`mdast-util-to-hast`][mdast-util-to-hast], turns text (inline) math nodes into
`<span class="math math-inline">…</span>` and flow (block) math nodes into
`<div class="math math-display">…</div>`.
`<code class="language-math math-inline">…</code>` and flow (block) math nodes
into `<pre><code class="language-math math-display">…</code></pre>`.

## Install

Expand Down Expand Up @@ -174,8 +174,8 @@ Single dollars work in Pandoc and many other places, but often interfere with

This plugin integrates with [`mdast-util-to-hast`][mdast-util-to-hast].
When mdast is turned into hast the math nodes are turned into
`<span class="math math-inline">…</span>` and
`<div class="math math-display">…</div>` elements.
`<code class="language-math math-inline">…</code>` and
`<pre><code class="language-math math-display">…</code></pre>`.

## Syntax tree

Expand Down
34 changes: 24 additions & 10 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ test('markdown -> mdast', (t) => {
type: 'inlineMath',
value: 'b',
data: {
hName: 'span',
hProperties: {className: ['math', 'math-inline']},
hName: 'code',
hProperties: {className: ['language-math', 'math-inline']},
hChildren: [{type: 'text', value: 'b'}]
},
position: {
Expand Down Expand Up @@ -70,9 +70,16 @@ test('markdown -> mdast', (t) => {
meta: null,
value: 'a',
data: {
hName: 'div',
hProperties: {className: ['math', 'math-display']},
hChildren: [{type: 'text', value: 'a'}]
hName: 'pre',
hProperties: {},
hChildren: [
{
type: 'element',
tagName: 'code',
properties: {className: ['language-math', 'math-display']},
children: [{type: 'text', value: 'a'}]
}
]
},
position: {
start: {line: 1, column: 1, offset: 0},
Expand All @@ -92,9 +99,16 @@ test('markdown -> mdast', (t) => {
meta: 'a&b&c',
value: '',
data: {
hName: 'div',
hProperties: {className: ['math', 'math-display']},
hChildren: [{type: 'text', value: ''}]
hName: 'pre',
hProperties: {},
hChildren: [
{
type: 'element',
tagName: 'code',
properties: {className: ['language-math', 'math-display']},
children: [{type: 'text', value: ''}]
}
]
},
position: {
start: {line: 1, column: 1, offset: 0},
Expand All @@ -116,8 +130,8 @@ test('markdown -> mdast', (t) => {
type: 'inlineMath',
value: 'a\nb\nb',
data: {
hName: 'span',
hProperties: {className: ['math', 'math-inline']},
hName: 'code',
hProperties: {className: ['language-math', 'math-inline']},
hChildren: [{type: 'text', value: 'a\nb\nb'}]
},
position: {
Expand Down