Skip to content

Commit 9052f3e

Browse files
committed
Handle another common invalid JSON case
1 parent 90be83d commit 9052f3e

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/parsers/jsonld-parser.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,18 @@ export default function (html, config = {}) {
77

88
$html('script[type="application/ld+json"]').each((index, item) => {
99
try {
10-
const json = item.children[0].data.trim().replace(/[\r\n]/g, ' ').replace(/;$/, '')
11-
let parsedJSON = JSON.parse(json)
10+
const json = item.children[0].data
11+
const cleanedJson = json
12+
// Trim whitespace
13+
.trim()
14+
// Handle invalid escpe characters
15+
.replace(/([^bfrnt\\/"])\\([^bfrnt\\/"])/ig, '$1\\\\$2')
16+
// Strip line breaks
17+
.replace(/[\r\n]/g, ' ')
18+
// Remove trailing semicolon
19+
.replace(/;$/, '')
20+
21+
let parsedJSON = JSON.parse(cleanedJson)
1222
if (!Array.isArray(parsedJSON)) {
1323
parsedJSON = [parsedJSON]
1424
}

0 commit comments

Comments
 (0)