Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/markdown-exit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"prepublishOnly": "pnpm build"
},
"dependencies": {
"entities": "catalog:integrations",
"character-entities": "catalog:integrations",
"linkify-it": "catalog:integrations",
"mdurl": "catalog:integrations",
"punycode.js": "catalog:integrations",
Expand Down
7 changes: 7 additions & 0 deletions packages/markdown-exit/src/common/entities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { characterEntities } from 'character-entities'

const own = Object.prototype.hasOwnProperty

export function decodeNamedEntity(name: string): string | undefined {
return own.call(characterEntities, name) ? characterEntities[name] : undefined
}
9 changes: 2 additions & 7 deletions packages/markdown-exit/src/common/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Utilities
//

import { decodeHTMLStrict } from 'entities'
import * as mdurl from 'mdurl'
import * as ucmicro from 'uc.micro'
import { decodeNamedEntity } from './entities'

type ClassToWrap = new (...args: any[]) => object

Expand Down Expand Up @@ -90,12 +90,7 @@ function replaceEntityPattern(match: string, name: string): string {
return match
}

const decoded = decodeHTMLStrict(match)
if (decoded !== match) {
return decoded
}

return match
return decodeNamedEntity(name) ?? match
}

export function unescapeMd(str: string): string {
Expand Down
6 changes: 3 additions & 3 deletions packages/markdown-exit/src/parser/inline/rules/entity.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Process html entity - {, ¯, ", ...

import type StateInline from '../state_inline'
import { decodeHTMLStrict } from 'entities'
import { decodeNamedEntity } from '../../../common/entities'
import { fromCodePoint, isValidEntityCode } from '../../../common/utils'

const DIGITAL_RE = /^&#(x[a-f0-9]{1,6}|\d{1,7});/i
Expand Down Expand Up @@ -36,8 +36,8 @@ export default function entity(state: StateInline, silent: boolean) {
} else {
const match = state.src.slice(pos).match(NAMED_RE)
if (match) {
const decoded = decodeHTMLStrict(match[0])
if (decoded !== match[0]) {
const decoded = decodeNamedEntity(match[1])
if (decoded !== undefined) {
if (!silent) {
const token = state.push('text_special', '', 0)
token.content = decoded
Expand Down
27 changes: 27 additions & 0 deletions packages/markdown-exit/tests/misc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,33 @@ describe('misc', () => {
assert.strictEqual(md.render(' codeblock\n'), '<pre><code>codeblock\n</code></pre>\n')
})

it('should keep invalid entity references in link destinations and titles', () => {
const md = MarkdownExit()

assert.strictEqual(
md.render('[a](/x&ampfoo;y&#000000065; "t&notit;")'),
'<p><a href="/x&amp;ampfoo;y&amp;#000000065;" title="t&amp;notit;">a</a></p>\n',
)
})

it('should keep unknown entity names literal', () => {
const md = MarkdownExit()

assert.strictEqual(
md.render('&pm1; &acy1; [a](/x&pm1;y "t&acy1;")'),
'<p>&amp;pm1; &amp;acy1; <a href="/x&amp;pm1;y" title="t&amp;acy1;">a</a></p>\n',
)
})

it('should not decode Object.prototype keys as entities', () => {
const md = MarkdownExit()

assert.strictEqual(
md.render('&constructor; &toString; [a](/&hasOwnProperty; "&valueOf;")'),
'<p>&amp;constructor; &amp;toString; <a href="/&amp;hasOwnProperty;" title="&amp;valueOf;">a</a></p>\n',
)
})

it('should quickly exit on empty string', () => {
const md = MarkdownExit()

Expand Down
16 changes: 5 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ catalogs:
vitepress: 2.0.0-alpha.12
vue: ^3.5.26
integrations:
entities: ^8.0.0
character-entities: ^2.0.2
linkify-it: ^6.0.0
mdurl: ^2.1.0
punycode.js: ^2.3.1
Expand Down