All files / src/lib/markdown-it/utils index.ts

100% Statements 6/6
100% Branches 0/0
100% Functions 2/2
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35                10x                                     10x 1x 1x 5x   1x    
/*
  Extra utility functions related to markdown-it.
  markdown-it library exposes a utility module in markdown-it/utils,
  below are additional functions that can be used as helpers alongside markdown-it/utils
 */
 
// This mapping is taken from markdown-it/utils, just flipped.
// Refer to the original file at markdown-it/lib/common/utils.js
const htmlUnescapedMapping = {
  '&': '&',
  '&lt;': '<',
  '&gt;': '>',
  '&quot;': '"',
  '&#x27;': '\'',
};
 
// markdown-it/utils have an escapeHtml function, but not the
// complementary un-escaping function
 
// Used in highlighting calculations since
// markdown-it stores text as escaped HTML entities (e.g. &lt;, &gt;).
// To correctly measure character positions and split text for partial
// highlights, we must first decode entities back into their real characters.
 
/**
 * Replaces HTML escape sequences in the input string with their corresponding unescaped characters.
 */
export function unescapeHtml(str: string) {
  let unescaped = str;
  Object.entries(htmlUnescapedMapping).forEach(([key, value]) => {
    unescaped = unescaped.split(key).join(value);
  });
  return unescaped;
}