Skip to content

Commit

Permalink
Add compilePattern helper to state
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 16, 2023
1 parent f4fd134 commit 5fd2f1e
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 9 deletions.
4 changes: 1 addition & 3 deletions lib/handle/inline-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* @typedef {import('../types.js').State} State
*/

import {patternCompile} from '../util/pattern-compile.js'

inlineCode.peek = inlineCodePeek

/**
Expand Down Expand Up @@ -44,7 +42,7 @@ export function inlineCode(node, _, state) {
// them out.
while (++index < state.unsafe.length) {
const pattern = state.unsafe[index]
const expression = patternCompile(pattern)
const expression = state.compilePattern(pattern)
/** @type {RegExpExecArray | null} */
let match

Expand Down
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {handle as handlers} from './handle/index.js'
import {join} from './join.js'
import {unsafe} from './unsafe.js'
import {association} from './util/association.js'
import {compilePattern} from './util/compile-pattern.js'
import {containerPhrasing} from './util/container-phrasing.js'
import {containerFlow} from './util/container-flow.js'
import {indentLines} from './util/indent-lines.js'
Expand All @@ -42,6 +43,7 @@ export function toMarkdown(tree, options = {}) {
containerPhrasing: containerPhrasingBound,
containerFlow: containerFlowBound,
createTracker: track,
compilePattern,
safe: safeBound,
stack: [],
unsafe: [...unsafe],
Expand Down
9 changes: 9 additions & 0 deletions lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@
* @returns {Tracker}
* Tracker.
*
* @callback CompilePattern
* Compile an unsafe pattern to a regex.
* @param {Unsafe} info
* Pattern.
* @returns {RegExp}
* Regex.
*
* @callback AssociationId
* Get an identifier from an association to match it to others.
*
Expand Down Expand Up @@ -195,6 +202,8 @@
* Pad serialized markdown.
* @property {AssociationId} associationId
* Get an identifier from an association to match it to others.
* @property {CompilePattern} compilePattern
* Compile an unsafe pattern to a regex.
* @property {ContainerPhrasing} containerPhrasing
* Serialize the children of a parent that contains phrasing children.
* @property {ContainerFlow} containerFlow
Expand Down
7 changes: 3 additions & 4 deletions lib/util/pattern-compile.js → lib/util/compile-pattern.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/**
* @typedef {import('../types.js').Unsafe} Unsafe
* @typedef {import('../types.js').CompilePattern} CompilePattern
*/

/**
* @param {Unsafe} pattern
* @returns {RegExp}
* @type {CompilePattern}
*/
export function patternCompile(pattern) {
export function compilePattern(pattern) {
if (!pattern._compiled) {
const before =
(pattern.atBreak ? '[\\r\\n][\\t ]*' : '') +
Expand Down
3 changes: 1 addition & 2 deletions lib/util/safe.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* @typedef {import('../types.js').State} State
*/

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

/**
Expand Down Expand Up @@ -48,7 +47,7 @@ export function safe(state, input, config) {
continue
}

const expression = patternCompile(pattern)
const expression = state.compilePattern(pattern)
/** @type {RegExpExecArray | null} */
let match

Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,8 @@ Info passed around about the current state (TypeScript type).
(see [`ConstructName`][api-construct-name])
* `indentLines` (`(value: string, map: Map) => string`)
— pad serialized markdown (see [`Map`][api-map])
* `compilePattern` (`(pattern: Unsafe) => RegExp`)
— compile an unsafe pattern to a regex (see [`Unsafe`][api-unsafe])
* `containerFlow` (`(parent: Node, info: Info) => string`)
— serialize flow children (see [`Info`][api-info])
* `containerPhrasing` (`(parent: Node, info: Info) => string`)
Expand Down

0 comments on commit 5fd2f1e

Please sign in to comment.