Skip to content

Commit

Permalink
fix: ignore words break fences, #45
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Dec 15, 2024
1 parent 5746f01 commit eb92ac0
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/nodes/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Node } from './node'

export type MathDelimiter = '$' | '$$'

export class Math implements Node {
export class MathNode implements Node {
readonly children: Node[] = []
readonly code: string
readonly kind = NodeKind.Math
Expand Down
4 changes: 2 additions & 2 deletions src/nodes/type-guards.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { InlineCode } from './inline-code'
import { Math } from './math'
import { MathNode } from './math'
import { ReferenceLink } from './reference-link'
import { Punctuation } from './punctuation'
import { UnicodeString } from './unicode-string'
Expand Down Expand Up @@ -48,7 +48,7 @@ export function isLatin (node: Node) {
return isUnicodeString(node) || isAlphabetNumeric(node)
}

export function isMath (node: Node): node is Math {
export function isMath (node: Node): node is MathNode {
return node.kind === NodeKind.Math
}

Expand Down
22 changes: 22 additions & 0 deletions src/parser/ignore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { NormalizedPadMarkdownOptions } from '../transformers/pad-markdown-options'

export function preprocessIgnores (str: string, options: NormalizedPadMarkdownOptions) {
const lengths = new Map()
for (const word of options.ignoreWords) {
let index = -1
let begin = 0
while ((index = str.indexOf(word, begin)) !== -1) {
const len = lengths.get(index) ?? 0
lengths.set(index, Math.max(len, word.length))
begin = index + word.length
}
}
for (const pattern of options.ignorePatterns) {
let match: RegExpMatchArray | null = null
while ((match = pattern.exec(str)) !== null) {
const len = lengths.get(match.index) ?? 0
lengths.set(match.index, Math.max(len, match[0].length))
}
}
return lengths
}
45 changes: 15 additions & 30 deletions src/parser/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { ReferenceDefinition } from '../nodes/reference-definition'
import { InlineLink } from '../nodes/inline-link'
import { Node } from '../nodes/node'
import { InlineCode, InlineCodeDelimiter } from '../nodes/inline-code'
import { Math, MathDelimiter } from '../nodes/math'
import { MathNode, MathDelimiter } from '../nodes/math'
import { BlockCode, BlockCodeDelimiter } from '../nodes/block-code'
import { Blank } from '../nodes/blank'
import { Raw } from '../nodes/raw'
Expand All @@ -37,6 +37,7 @@ import { parseCode } from './parse-code'
import { matchSubstring } from '../utils/string'
import { NormalizedPadMarkdownOptions } from '../transformers/pad-markdown-options'
import { CJK } from '../nodes/cjk'
import { preprocessIgnores } from './ignore'

const RAW_BEGIN = '<!--md-padding-ignore-begin-->'
const RAW_END = '<!--md-padding-ignore-end-->'
Expand All @@ -46,25 +47,9 @@ const enum ForceCloseResult {
ReParse,
Error
}

export function parse (str: string, options: NormalizedPadMarkdownOptions): Document {
if (options.ignorePatterns.length) {
const pattern = options.ignorePatterns[0]!
const newOptions = { ...options, ignorePatterns: options.ignorePatterns.slice(1) }
const children: Node[] = []
const ignores = str.matchAll(pattern)
let prev = 0
for (const match of ignores) {
for (const child of parse(str.slice(prev, match.index), newOptions).children) {
children.push(child)
}
children.push(new Raw(match[0]))
prev = match.index! + match[0].length
}
for (const child of parse(str.slice(prev, str.length), newOptions).children) {
children.push(child)
}
return compactTree(new Document(children))
}
const ignoreLengths = preprocessIgnores(str, options)
const stack = new Stack<Context>()
const mask = new Mask()

Expand Down Expand Up @@ -97,6 +82,9 @@ export function parse (str: string, options: NormalizedPadMarkdownOptions): Docu
resolve(Punctuation.create(str[i + 1], str.slice(i, i + 2)))
i += 2
}
else if (handleIgnores()) {
// do nothing, already handled
}

// Inline Code
else if (state === State.InlineCode && matchSubstring(str, i, inlineCodeDelimiter)) {
Expand All @@ -119,9 +107,9 @@ export function parse (str: string, options: NormalizedPadMarkdownOptions): Docu
i += 3
}

// Math
// MathNode
else if (state === State.Math && matchSubstring(str, i, mathDelimiter)) {
resolve(new Math(popMarkdown(), mathDelimiter))
resolve(new MathNode(popMarkdown(), mathDelimiter))
i += mathDelimiter.length
}

Expand Down Expand Up @@ -365,9 +353,7 @@ export function parse (str: string, options: NormalizedPadMarkdownOptions): Docu
return compactTree(new Document(popNodes()))

function handleText (c: string) {
if (handleIgnores()) {
// do nothing, already handled
} else if (Punctuation.is(c)) {
if (Punctuation.is(c)) {
resolve(Punctuation.create(c))
i++
} else if (Blank.is(c)) {
Expand All @@ -386,12 +372,11 @@ export function parse (str: string, options: NormalizedPadMarkdownOptions): Docu
}

function handleIgnores (): boolean {
for (const ignore of options.ignoreWords) {
if (matchSubstring(str, i, ignore)) {
resolve(new Raw(ignore))
i += ignore.length
return true
}
const len = ignoreLengths.get(i)
if (len) {
resolve(new Raw(str.slice(i, i + len)))
i += len
return true
}
return false
}
Expand Down
5 changes: 5 additions & 0 deletions test/transformers/pad-markdown.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ describe('padding()', () => {
const ignorePatterns = ['<u>.+?</u>', '\\*\\*.+?\\*\\*']
expect(padMarkdown(src, { ignorePatterns })).toEqual(src)
})
it('should support ignorePatterns inside code fences', () => {
const src = '```html\n<br/>\nlet a=3;```\nfoo'
const ignorePatterns = ['<br/>']
expect(padMarkdown(src, { ignorePatterns })).toEqual(src)
})
})

describe('emphasis, strong, strikethrough', () => {
Expand Down

0 comments on commit eb92ac0

Please sign in to comment.