Skip to content

Commit

Permalink
fix: allow escape by \
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed May 7, 2024
1 parent 3b6a233 commit 9c89330
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
14 changes: 8 additions & 6 deletions src/nodes/punctuation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import { isAlphabetNumeric, isPunctuation } from './type-guards'
export class Punctuation implements Node {
readonly children: Node[] = []
readonly char: string
readonly raw: string
readonly kind = NodeKind.Punctuation
private static cache = new Map<string, Punctuation>()

private constructor (char: string) {
private constructor (char: string, raw?: string) {
this.char = char
this.raw = raw ?? char
}

needPaddingAfter (next: Node, prev?: Node) {
Expand Down Expand Up @@ -46,15 +48,15 @@ export class Punctuation implements Node {
}

toMarkdown () {
return this.char
return this.raw
}

// create a flyweight punctuation
static create (char: string): Punctuation {
if (!Punctuation.cache.has(char)) {
Punctuation.cache.set(char, new Punctuation(char))
static create (char: string, raw = char): Punctuation {
if (!Punctuation.cache.has(raw)) {
Punctuation.cache.set(raw, new Punctuation(char, raw))
}
return Punctuation.cache.get(char)!
return Punctuation.cache.get(raw)!
}

static is (char: any): char is string {
Expand Down
10 changes: 7 additions & 3 deletions src/parser/parse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isBlank, isWordBoundary } from '../utils/char'
import { isBlank, isWordBoundary, markdownSpecial } from '../utils/char'
import { isBlank as isBlankNode } from '../nodes/type-guards'
import { InlineImage } from '../nodes/inline-image'
import { ReferenceImage } from '../nodes/reference-image'
Expand Down Expand Up @@ -43,7 +43,6 @@ const enum ForceCloseResult {
ReParse,
Error
}
let count = 0
export function parse (str: string, options :NormalizedPadMarkdownOptions): Document {
const stack = new Stack<Context>()
const mask = new Mask()
Expand Down Expand Up @@ -73,8 +72,13 @@ export function parse (str: string, options :NormalizedPadMarkdownOptions): Docu

if (c === '\n' && forceCloseAllInlineNodes() === ForceCloseResult.ReParse) continue

if (c === '\\' && markdownSpecial.has(str[i + 1])) {
resolve(Punctuation.create(str[i + 1], str.slice(i, i + 2)))
i += 2
}

// Inline Code
if (state === State.InlineCode && matchSubstring(str, i, inlineCodeDelimiter)) {
else if (state === State.InlineCode && matchSubstring(str, i, inlineCodeDelimiter)) {
resolve(new InlineCode(popMarkdown(), inlineCodeDelimiter))
i += inlineCodeDelimiter.length
}
Expand Down
11 changes: 11 additions & 0 deletions src/utils/char.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ const rPunctuation = /^[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,\-./:;<=>?@[\]^_
// '·' need to be treated as full-width as will be full in some fonts
const rFullwidthPunctuation = /^[、,:。?!;:【】()「」﹁﹂『』《》〈〉“”‘’﹏…—~‧·]$/

export const markdownSpecial = new Set([
'*',
'-',
'[', ']',
'(', ')',
'<', '>',
'"', "'",
'!',
'=',
'$',

Check failure on line 22 in src/utils/char.ts

View workflow job for this annotation

GitHub Actions / Check

Unexpected trailing comma
])
export function isPunctuationCharacter (char: any) {
if (typeof char !== 'string') return false

Expand Down
7 changes: 7 additions & 0 deletions test/transformers/pad-markdown.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,11 @@ describe('padding()', () => {
expect(padMarkdown(input)).toEqual(output)
})
})
describe('escape', () => {
it('should allow \\ escape *', () => {
expect(padMarkdown('*foo*bar')).toEqual('*foo* bar')
expect(padMarkdown('foo*bar')).toEqual('foo*bar')
expect(padMarkdown('\\*foo\\*bar')).toEqual('\\*foo\\*bar')
})
})
})

0 comments on commit 9c89330

Please sign in to comment.