Skip to content

Commit 15b3e8e

Browse files
committed
Change to use pre, code elements in HTML
Closes GH-2.
1 parent 708ed11 commit 15b3e8e

File tree

4 files changed

+65
-32
lines changed

4 files changed

+65
-32
lines changed

Diff for: lib/index.js

+22-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/**
2+
* @typedef {import('hast').Element} HastElement
3+
* @typedef {import('hast').ElementContent} HastElementContent
24
* @typedef {import('mdast-util-from-markdown').CompileContext} CompileContext
35
* @typedef {import('mdast-util-from-markdown').Extension} FromMarkdownExtension
46
* @typedef {import('mdast-util-from-markdown').Handle} FromMarkdownHandle
@@ -48,16 +50,19 @@ export function mathFromMarkdown() {
4850
* @type {FromMarkdownHandle}
4951
*/
5052
function enterMathFlow(token) {
53+
/** @type {HastElement} */
54+
const code = {
55+
type: 'element',
56+
tagName: 'code',
57+
properties: {className: ['language-math', 'math-display']},
58+
children: []
59+
}
5160
this.enter(
5261
{
5362
type: 'math',
5463
meta: null,
5564
value: '',
56-
data: {
57-
hName: 'div',
58-
hProperties: {className: ['math', 'math-display']},
59-
hChildren: [{type: 'text', value: ''}]
60-
}
65+
data: {hName: 'pre', hChildren: [code]}
6166
},
6267
token
6368
)
@@ -104,7 +109,10 @@ export function mathFromMarkdown() {
104109
this.exit(token)
105110
node.value = data
106111
// @ts-expect-error: we defined it in `enterMathFlow`.
107-
node.data.hChildren[0].value = data
112+
const code = /** @type {HastElement} */ (node.data.hChildren[0])
113+
assert(code.type === 'element')
114+
assert(code.tagName === 'code')
115+
code.children.push({type: 'text', value: data})
108116
this.data.mathFlowInside = undefined
109117
}
110118

@@ -118,9 +126,9 @@ export function mathFromMarkdown() {
118126
type: 'inlineMath',
119127
value: '',
120128
data: {
121-
hName: 'span',
122-
hProperties: {className: ['math', 'math-inline']},
123-
hChildren: [{type: 'text', value: ''}]
129+
hName: 'code',
130+
hProperties: {className: ['language-math', 'math-inline']},
131+
hChildren: []
124132
}
125133
},
126134
token
@@ -138,8 +146,11 @@ export function mathFromMarkdown() {
138146
assert(node.type === 'inlineMath')
139147
this.exit(token)
140148
node.value = data
141-
// @ts-expect-error: we defined it.
142-
node.data.hChildren[0].value = data
149+
const children = /** @type {Array<HastElementContent>} */ (
150+
// @ts-expect-error: we defined it in `enterMathFlow`.
151+
node.data.hChildren
152+
)
153+
children.push({type: 'text', value: data})
143154
}
144155

145156
/**

Diff for: package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@
3535
"index.js"
3636
],
3737
"dependencies": {
38+
"@types/hast": "^3.0.0",
3839
"@types/mdast": "^4.0.0",
3940
"devlop": "^1.0.0",
4041
"longest-streak": "^3.0.0",
4142
"mdast-util-from-markdown": "^2.0.0",
42-
"mdast-util-to-markdown": "^2.1.0"
43+
"mdast-util-to-markdown": "^2.1.0",
44+
"unist-util-remove-position": "^5.0.0"
4345
},
4446
"devDependencies": {
4547
"@types/node": "^20.0.0",

Diff for: readme.md

+18-10
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ internals away.
6868
This utility adds [fields on nodes][fields] so that the utility responsible for
6969
turning mdast (markdown) nodes into hast (HTML) nodes,
7070
[`mdast-util-to-hast`][mdast-util-to-hast], turns text (inline) math nodes into
71-
`<span class="math math-inline">…</span>` and flow (block) math nodes into
72-
`<div class="math math-display">…</div>`.
71+
`<code class="language-math math-inline">…</code>` and flow (block) math nodes
72+
into `<pre><code class="language-math math-display">…</code></pre>`.
7373

7474
## Install
7575

@@ -99,7 +99,8 @@ In browsers with [`esm.sh`][esmsh]:
9999
Say our document `example.md` contains:
100100

101101
```markdown
102-
Lift($L$) can be determined by Lift Coefficient ($C_L$) like the following equation.
102+
Lift($L$) can be determined by Lift Coefficient ($C_L$) like the following
103+
equation.
103104

104105
$$
105106
L = \frac{1}{2} \rho v^2 S C_L
@@ -142,8 +143,8 @@ brevity):
142143
{type: 'text', value: 'Lift('},
143144
{type: 'inlineMath', value: 'L', data: {/**/}},
144145
{type: 'text', value: ') can be determined by Lift Coefficient ('},
145-
{type: 'inlineMath', e: 'C_L', data: {/**/}},
146-
{type: 'text', value: ') like the following equation.'}
146+
{type: 'inlineMath', value: 'C_L', data: {/**/}},
147+
{type: 'text', value: ') like the following\nequation.'}
147148
]
148149
},
149150
{type: 'math', meta: null, value: 'L = \\frac{1}{2} \\rho v^2 S C_L', data: {/**/}}
@@ -152,7 +153,8 @@ brevity):
152153
```
153154

154155
```markdown
155-
Lift($L$) can be determined by Lift Coefficient ($C_L$) like the following equation.
156+
Lift($L$) can be determined by Lift Coefficient ($C_L$) like the following
157+
equation.
156158

157159
$$
158160
L = \frac{1}{2} \rho v^2 S C_L
@@ -196,11 +198,14 @@ Math (text) (TypeScript type).
196198
###### Type
197199

198200
```ts
199-
import type {Literal} from 'mdast'
201+
import type {Data, Literal} from 'mdast'
200202

201203
interface InlineMath extends Literal {
202204
type: 'inlineMath'
205+
data?: InlineMathData | undefined
203206
}
207+
208+
export interface InlineMathData extends Data {}
204209
```
205210

206211
### `Math`
@@ -210,12 +215,15 @@ Math (flow) (TypeScript type).
210215
###### Type
211216

212217
```ts
213-
import type {Literal} from 'mdast'
218+
import type {Data, Literal} from 'mdast'
214219

215220
interface Math extends Literal {
216221
type: 'math'
217222
meta?: string | null | undefined
223+
data?: MathData | undefined
218224
}
225+
226+
export interface MathData extends Data {}
219227
```
220228

221229
### `ToOptions`
@@ -234,8 +242,8 @@ Configuration (TypeScript type).
234242

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

240248
## Syntax
241249

Diff for: test.js

+22-10
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ test('mathFromMarkdown', async function (t) {
3939
type: 'inlineMath',
4040
value: 'b',
4141
data: {
42-
hName: 'span',
43-
hProperties: {className: ['math', 'math-inline']},
42+
hName: 'code',
43+
hProperties: {className: ['language-math', 'math-inline']},
4444
hChildren: [{type: 'text', value: 'b'}]
4545
},
4646
position: {
@@ -82,9 +82,15 @@ test('mathFromMarkdown', async function (t) {
8282
meta: null,
8383
value: 'a',
8484
data: {
85-
hName: 'div',
86-
hProperties: {className: ['math', 'math-display']},
87-
hChildren: [{type: 'text', value: 'a'}]
85+
hName: 'pre',
86+
hChildren: [
87+
{
88+
type: 'element',
89+
tagName: 'code',
90+
properties: {className: ['language-math', 'math-display']},
91+
children: [{type: 'text', value: 'a'}]
92+
}
93+
]
8894
},
8995
position: {
9096
start: {line: 1, column: 1, offset: 0},
@@ -105,9 +111,15 @@ test('mathFromMarkdown', async function (t) {
105111
meta: 'a&b&c',
106112
value: '',
107113
data: {
108-
hName: 'div',
109-
hProperties: {className: ['math', 'math-display']},
110-
hChildren: [{type: 'text', value: ''}]
114+
hName: 'pre',
115+
hChildren: [
116+
{
117+
type: 'element',
118+
tagName: 'code',
119+
properties: {className: ['language-math', 'math-display']},
120+
children: [{type: 'text', value: ''}]
121+
}
122+
]
111123
},
112124
position: {
113125
start: {line: 1, column: 1, offset: 0},
@@ -130,8 +142,8 @@ test('mathFromMarkdown', async function (t) {
130142
type: 'inlineMath',
131143
value: 'a\nb\nb',
132144
data: {
133-
hName: 'span',
134-
hProperties: {className: ['math', 'math-inline']},
145+
hName: 'code',
146+
hProperties: {className: ['language-math', 'math-inline']},
135147
hChildren: [{type: 'text', value: 'a\nb\nb'}]
136148
},
137149
position: {

0 commit comments

Comments
 (0)