Skip to content

Commit d22f71d

Browse files
committed
Refactor code-style
1 parent 0b1cb1e commit d22f71d

File tree

5 files changed

+441
-350
lines changed

5 files changed

+441
-350
lines changed

index.d.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export type {ToOptions} from './lib/index.js'
77
/**
88
* Math (flow).
99
*/
10-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
1110
export interface Math extends Literal {
1211
/**
1312
* Node type.
@@ -17,13 +16,12 @@ export interface Math extends Literal {
1716
/**
1817
* Custom information relating to the node.
1918
*/
20-
meta?: string | undefined | null
19+
meta?: string | null | undefined
2120
}
2221

2322
/**
2423
* Math (text).
2524
*/
26-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
2725
export interface InlineMath extends Literal {
2826
/**
2927
* Node type.
@@ -33,7 +31,6 @@ export interface InlineMath extends Literal {
3331

3432
// Add custom data tracked to turn markdown into a tree.
3533
declare module 'mdast-util-from-markdown' {
36-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
3734
interface CompileData {
3835
/**
3936
* Whether we’re in math (flow).
@@ -44,7 +41,6 @@ declare module 'mdast-util-from-markdown' {
4441

4542
// Add custom data tracked to turn a tree into markdown.
4643
declare module 'mdast-util-to-markdown' {
47-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
4844
interface ConstructNameMap {
4945
/**
5046
* Math (flow).
@@ -76,17 +72,14 @@ declare module 'mdast-util-to-markdown' {
7672

7773
// Add nodes to tree.
7874
declare module 'mdast' {
79-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
8075
interface BlockContentMap {
8176
math: Math
8277
}
8378

84-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
8579
interface PhrasingContentMap {
8680
inlineMath: InlineMath
8781
}
8882

89-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
9083
interface RootContentMap {
9184
inlineMath: InlineMath
9285
math: Math

lib/index.js

+22-22
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@
22
* @typedef {import('mdast-util-from-markdown').CompileContext} CompileContext
33
* @typedef {import('mdast-util-from-markdown').Extension} FromMarkdownExtension
44
* @typedef {import('mdast-util-from-markdown').Handle} FromMarkdownHandle
5-
* @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension
65
* @typedef {import('mdast-util-to-markdown').Handle} ToMarkdownHandle
7-
* @typedef {import('../index.js').Math} Math
6+
* @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension
87
* @typedef {import('../index.js').InlineMath} InlineMath
8+
* @typedef {import('../index.js').Math} Math
99
*
1010
* @typedef ToOptions
1111
* Configuration.
1212
* @property {boolean | null | undefined} [singleDollarTextMath=true]
13-
* Whether to support math (text) with a single dollar.
13+
* Whether to support math (text) with a single dollar (default: `true`).
1414
*
1515
* Single dollars work in Pandoc and many other places, but often interfere
1616
* with “normal” dollars in text.
1717
* If you turn this off, you can still use two or more dollars for text math.
1818
*/
1919

20+
import {ok as assert} from 'devlop'
2021
import {longestStreak} from 'longest-streak'
2122

2223
/**
@@ -76,7 +77,8 @@ export function mathFromMarkdown() {
7677
*/
7778
function exitMathFlowMeta() {
7879
const data = this.resume()
79-
const node = /** @type {Math} */ (this.stack[this.stack.length - 1])
80+
const node = this.stack[this.stack.length - 1]
81+
assert(node.type === 'math')
8082
node.meta = data
8183
}
8284

@@ -97,10 +99,11 @@ export function mathFromMarkdown() {
9799
*/
98100
function exitMathFlow(token) {
99101
const data = this.resume().replace(/^(\r?\n|\r)|(\r?\n|\r)$/g, '')
100-
const node = /** @type {Math} */ (this.stack[this.stack.length - 1])
102+
const node = this.stack[this.stack.length - 1]
103+
assert(node.type === 'math')
101104
this.exit(token)
102105
node.value = data
103-
// @ts-expect-error: we defined it.
106+
// @ts-expect-error: we defined it in `enterMathFlow`.
104107
node.data.hChildren[0].value = data
105108
this.data.mathFlowInside = undefined
106109
}
@@ -131,7 +134,8 @@ export function mathFromMarkdown() {
131134
*/
132135
function exitMathText(token) {
133136
const data = this.resume()
134-
const node = /** @type {Math} */ (this.stack[this.stack.length - 1])
137+
const node = this.stack[this.stack.length - 1]
138+
assert(node.type === 'inlineMath')
135139
this.exit(token)
136140
node.value = data
137141
// @ts-expect-error: we defined it.
@@ -152,7 +156,7 @@ export function mathFromMarkdown() {
152156
* Create an extension for `mdast-util-to-markdown`.
153157
*
154158
* @param {ToOptions | null | undefined} [options]
155-
* Configuration.
159+
* Configuration (optional).
156160
* @returns {ToMarkdownExtension}
157161
* Extension for `mdast-util-to-markdown`.
158162
*/
@@ -184,22 +188,21 @@ export function mathToMarkdown(options) {
184188
* @type {ToMarkdownHandle}
185189
* @param {Math} node
186190
*/
187-
// To do: next major: rename `context` to state, `safeOptions` to info.
188191
// Note: fixing this code? Please also fix the similar code for code:
189192
// <https://github.com/syntax-tree/mdast-util-to-markdown/blob/main/lib/handle/code.js>
190-
function math(node, _, context, safeOptions) {
193+
function math(node, _, state, info) {
191194
const raw = node.value || ''
192-
const tracker = context.createTracker(safeOptions)
195+
const tracker = state.createTracker(info)
193196
const sequence = '$'.repeat(Math.max(longestStreak(raw, '$') + 1, 2))
194-
const exit = context.enter('mathFlow')
197+
const exit = state.enter('mathFlow')
195198
let value = tracker.move(sequence)
196199

197200
if (node.meta) {
198-
const subexit = context.enter('mathFlowMeta')
201+
const subexit = state.enter('mathFlowMeta')
199202
value += tracker.move(
200-
context.safe(node.meta, {
201-
before: value,
203+
state.safe(node.meta, {
202204
after: '\n',
205+
before: value,
203206
encode: ['$'],
204207
...tracker.current()
205208
})
@@ -224,10 +227,7 @@ export function mathToMarkdown(options) {
224227
*/
225228
// Note: fixing this code? Please also fix the similar code for inline code:
226229
// <https://github.com/syntax-tree/mdast-util-to-markdown/blob/main/lib/handle/inline-code.js>
227-
//
228-
// To do: next major: rename `context` to state.
229-
// To do: next major: use `state` (`safe`, `track`, `patternCompile`).
230-
function inlineMath(node, _, context) {
230+
function inlineMath(node, _, state) {
231231
let value = node.value || ''
232232
let size = 1
233233

@@ -267,15 +267,15 @@ export function mathToMarkdown(options) {
267267
// We can’t escape characters in `inlineMath`, but because eols are
268268
// transformed to spaces when going from markdown to HTML anyway, we can swap
269269
// them out.
270-
while (++index < context.unsafe.length) {
271-
const pattern = context.unsafe[index]
270+
while (++index < state.unsafe.length) {
271+
const pattern = state.unsafe[index]
272272

273273
// Only look for `atBreak`s.
274274
// Btw: note that `atBreak` patterns will always start the regex at LF or
275275
// CR.
276276
if (!pattern.atBreak) continue
277277

278-
const expression = context.compilePattern(pattern)
278+
const expression = state.compilePattern(pattern)
279279
/** @type {RegExpExecArray | null} */
280280
let match
281281

package.json

+12-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
],
3939
"dependencies": {
4040
"@types/mdast": "^4.0.0",
41+
"devlop": "^1.0.0",
4142
"longest-streak": "^3.0.0",
4243
"mdast-util-from-markdown": "^2.0.0",
4344
"mdast-util-to-markdown": "^2.1.0"
@@ -81,9 +82,19 @@
8182
"strict": true
8283
},
8384
"xo": {
85+
"overrides": [
86+
{
87+
"files": [
88+
"**/*.ts"
89+
],
90+
"rules": {
91+
"@typescript-eslint/ban-types": "off",
92+
"@typescript-eslint/consistent-type-definitions": "off"
93+
}
94+
}
95+
],
8496
"prettier": true,
8597
"rules": {
86-
"@typescript-eslint/ban-types": "off",
8798
"unicorn/prefer-at": "off",
8899
"unicorn/prefer-string-replace-all": "off"
89100
}

readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ import type {Literal} from 'mdast'
213213

214214
interface Math extends Literal {
215215
type: 'math'
216-
meta?: string | undefined | null
216+
meta?: string | null | undefined
217217
}
218218
```
219219

@@ -350,7 +350,7 @@ import {visit} from 'unist-util-visit'
350350
/** @type {import('mdast').Root} */
351351
const tree = getMdastNodeSomeHow()
352352

353-
visit(tree, (node) => {
353+
visit(tree, function (node) {
354354
// `node` can now be one of the nodes for math.
355355
})
356356
```

0 commit comments

Comments
 (0)