|
350 | 350 | });
|
351 | 351 | }
|
352 | 352 | } else {
|
353 |
| - // get the textContent of the given env HTML |
354 |
| - var tempContainer = document.createElement('div'); |
355 |
| - tempContainer.innerHTML = env.content; |
356 |
| - var code = tempContainer.textContent; |
357 |
| - |
358 |
| - env.content = Prism.highlight(code, grammar, codeLang); |
| 353 | + env.content = Prism.highlight(textContent(env.content), grammar, codeLang); |
359 | 354 | }
|
360 | 355 | });
|
361 | 356 |
|
| 357 | + var tagPattern = RegExp(Prism.languages.markup.tag.pattern.source, 'gi'); |
| 358 | + |
| 359 | + /** |
| 360 | + * A list of known entity names. |
| 361 | + * |
| 362 | + * This will always be incomplete to save space. The current list is the one used by lowdash's unescape function. |
| 363 | + * |
| 364 | + * @see {@link https://github.com/lodash/lodash/blob/2da024c3b4f9947a48517639de7560457cd4ec6c/unescape.js#L2} |
| 365 | + */ |
| 366 | + var KNOWN_ENTITY_NAMES = { |
| 367 | + 'amp': '&', |
| 368 | + 'lt': '<', |
| 369 | + 'gt': '>', |
| 370 | + 'quot': '"', |
| 371 | + }; |
| 372 | + |
| 373 | + // IE 11 doesn't support `String.fromCodePoint` |
| 374 | + var fromCodePoint = String.fromCodePoint || String.fromCharCode; |
| 375 | + |
| 376 | + /** |
| 377 | + * Returns the text content of a given HTML source code string. |
| 378 | + * |
| 379 | + * @param {string} html |
| 380 | + * @returns {string} |
| 381 | + */ |
| 382 | + function textContent(html) { |
| 383 | + // remove all tags |
| 384 | + var text = html.replace(tagPattern, ''); |
| 385 | + |
| 386 | + // decode known entities |
| 387 | + text = text.replace(/&(\w{1,8}|#x?[\da-f]{1,8});/gi, function (m, code) { |
| 388 | + code = code.toLowerCase(); |
| 389 | + |
| 390 | + if (code[0] === '#') { |
| 391 | + var value; |
| 392 | + if (code[1] === 'x') { |
| 393 | + value = parseInt(code.slice(2), 16); |
| 394 | + } else { |
| 395 | + value = Number(code.slice(1)); |
| 396 | + } |
| 397 | + |
| 398 | + return fromCodePoint(value); |
| 399 | + } else { |
| 400 | + var known = KNOWN_ENTITY_NAMES[code]; |
| 401 | + if (known) { |
| 402 | + return known; |
| 403 | + } |
| 404 | + |
| 405 | + // unable to decode |
| 406 | + return m; |
| 407 | + } |
| 408 | + }); |
| 409 | + |
| 410 | + return text; |
| 411 | + } |
| 412 | + |
362 | 413 | Prism.languages.md = Prism.languages.markdown;
|
363 | 414 |
|
364 | 415 | }(Prism));
|
0 commit comments