Skip to content

Commit 5fd2f1e

Browse files
committed
Add compilePattern helper to state
1 parent f4fd134 commit 5fd2f1e

File tree

6 files changed

+18
-9
lines changed

6 files changed

+18
-9
lines changed

lib/handle/inline-code.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
* @typedef {import('../types.js').State} State
55
*/
66

7-
import {patternCompile} from '../util/pattern-compile.js'
8-
97
inlineCode.peek = inlineCodePeek
108

119
/**
@@ -44,7 +42,7 @@ export function inlineCode(node, _, state) {
4442
// them out.
4543
while (++index < state.unsafe.length) {
4644
const pattern = state.unsafe[index]
47-
const expression = patternCompile(pattern)
45+
const expression = state.compilePattern(pattern)
4846
/** @type {RegExpExecArray | null} */
4947
let match
5048

lib/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {handle as handlers} from './handle/index.js'
1717
import {join} from './join.js'
1818
import {unsafe} from './unsafe.js'
1919
import {association} from './util/association.js'
20+
import {compilePattern} from './util/compile-pattern.js'
2021
import {containerPhrasing} from './util/container-phrasing.js'
2122
import {containerFlow} from './util/container-flow.js'
2223
import {indentLines} from './util/indent-lines.js'
@@ -42,6 +43,7 @@ export function toMarkdown(tree, options = {}) {
4243
containerPhrasing: containerPhrasingBound,
4344
containerFlow: containerFlowBound,
4445
createTracker: track,
46+
compilePattern,
4547
safe: safeBound,
4648
stack: [],
4749
unsafe: [...unsafe],

lib/types.js

+9
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@
7575
* @returns {Tracker}
7676
* Tracker.
7777
*
78+
* @callback CompilePattern
79+
* Compile an unsafe pattern to a regex.
80+
* @param {Unsafe} info
81+
* Pattern.
82+
* @returns {RegExp}
83+
* Regex.
84+
*
7885
* @callback AssociationId
7986
* Get an identifier from an association to match it to others.
8087
*
@@ -195,6 +202,8 @@
195202
* Pad serialized markdown.
196203
* @property {AssociationId} associationId
197204
* Get an identifier from an association to match it to others.
205+
* @property {CompilePattern} compilePattern
206+
* Compile an unsafe pattern to a regex.
198207
* @property {ContainerPhrasing} containerPhrasing
199208
* Serialize the children of a parent that contains phrasing children.
200209
* @property {ContainerFlow} containerFlow

lib/util/pattern-compile.js renamed to lib/util/compile-pattern.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
/**
2-
* @typedef {import('../types.js').Unsafe} Unsafe
2+
* @typedef {import('../types.js').CompilePattern} CompilePattern
33
*/
44

55
/**
6-
* @param {Unsafe} pattern
7-
* @returns {RegExp}
6+
* @type {CompilePattern}
87
*/
9-
export function patternCompile(pattern) {
8+
export function compilePattern(pattern) {
109
if (!pattern._compiled) {
1110
const before =
1211
(pattern.atBreak ? '[\\r\\n][\\t ]*' : '') +

lib/util/safe.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* @typedef {import('../types.js').State} State
44
*/
55

6-
import {patternCompile} from './pattern-compile.js'
76
import {patternInScope} from './pattern-in-scope.js'
87

98
/**
@@ -48,7 +47,7 @@ export function safe(state, input, config) {
4847
continue
4948
}
5049

51-
const expression = patternCompile(pattern)
50+
const expression = state.compilePattern(pattern)
5251
/** @type {RegExpExecArray | null} */
5352
let match
5453

readme.md

+2
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,8 @@ Info passed around about the current state (TypeScript type).
478478
(see [`ConstructName`][api-construct-name])
479479
* `indentLines` (`(value: string, map: Map) => string`)
480480
— pad serialized markdown (see [`Map`][api-map])
481+
* `compilePattern` (`(pattern: Unsafe) => RegExp`)
482+
— compile an unsafe pattern to a regex (see [`Unsafe`][api-unsafe])
481483
* `containerFlow` (`(parent: Node, info: Info) => string`)
482484
— serialize flow children (see [`Info`][api-info])
483485
* `containerPhrasing` (`(parent: Node, info: Info) => string`)

0 commit comments

Comments
 (0)