@magic/transmute 0.0.61 → 0.0.62

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -368,6 +368,9 @@ update dependencies
368
368
  - sort imports
369
369
  - update dependencies
370
370
 
371
+ ##### 0.0.62
372
+
373
+ - markdown: escape html special characters in code and code span content. marked >= 5 no longer pre-escapes token text
371
374
 
372
375
  [npm-image]: https://img.shields.io/npm/v/@magic/transmute.sv
373
376
  [npm-url]: https://www.npmjs.com/package/@magic/transmute
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magic/transmute",
3
- "version": "0.0.61",
3
+ "version": "0.0.62",
4
4
  "description": "convert markdown and html to magic function strings.",
5
5
  "keywords": [
6
6
  "html",
@@ -61,16 +61,16 @@
61
61
  "@magic-modules/no-spy": "0.0.9",
62
62
  "@magic-modules/pre": "0.0.12",
63
63
  "@magic-themes/docs": "0.0.15",
64
- "@magic/core": "0.0.161",
64
+ "@magic/core": "0.0.163",
65
65
  "@magic/format": "0.0.82",
66
66
  "@magic/lint": "0.0.6",
67
- "@magic/test": "0.3.59"
67
+ "@magic/test": "0.3.60"
68
68
  },
69
69
  "engines": {
70
70
  "node": ">=14.15.4"
71
71
  },
72
72
  "engineStrict": true,
73
73
  "allowScripts": {
74
- "@swc/core@1.15.47": true
74
+ "@swc/core@1.16.22": true
75
75
  }
76
76
  }
package/src/lib/escape.js CHANGED
@@ -1,3 +1,9 @@
1
1
  // escape string delimiters in the string
2
2
  /** @param {string} str */
3
3
  export const escape = str => str.replace(/'/g, "\\'").replace(/`/g, '\\`').replace(/"/g, '\\"')
4
+
5
+ // escape html special characters in strings that are injected into html strings,
6
+ // so they render as text instead of being parsed as tags.
7
+ /** @param {string} str */
8
+ export const escapeHtml = str =>
9
+ str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
package/src/lib/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export { escape } from './escape.js'
1
+ export { escape, escapeHtml } from './escape.js'
2
2
  export { findState } from './findState.js'
3
3
  export { implantState } from './implantState.js'
4
4
  export { stringDecode } from './stringDecode.js'
@@ -1,4 +1,5 @@
1
1
  import { Renderer } from 'marked'
2
+ import { escapeHtml } from '../lib/index.js'
2
3
 
3
4
  export const markdown = new Renderer()
4
5
 
@@ -22,16 +23,17 @@ const slugify = str => {
22
23
  markdown.code = (/** @type {import('marked').Tokens.Code} */ token) => {
23
24
  const { text, lang } = token
24
25
  const language = (lang || '').match(/\S*/)?.[0]
26
+ const escaped = escapeHtml(text)
25
27
 
26
28
  if (!language) {
27
- return `<Pre>${text}</Pre>\n`
29
+ return `<Pre>${escaped}</Pre>\n`
28
30
  }
29
31
 
30
- return `<Pre class="${language}">${text}</Pre>`
32
+ return `<Pre class="${language}">${escaped}</Pre>`
31
33
  }
32
34
 
33
35
  markdown.codespan = (/** @type {import('marked').Tokens.Codespan} */ token) =>
34
- `<Pre lines="false">${token.text}</Pre>`
36
+ `<Pre lines="false">${escapeHtml(token.text)}</Pre>`
35
37
 
36
38
  markdown.link = (/** @type {import('marked').Tokens.Link} */ token) => {
37
39
  const { href, title, text } = token
@@ -158,7 +160,7 @@ markdown.paragraph = (/** @type {import('marked').Tokens.Paragraph} */ token) =>
158
160
  return out + '/>\n'
159
161
  }
160
162
  if (t.type === 'codespan') {
161
- return `<Pre lines="false">${t.text}</Pre>\n`
163
+ return `<Pre lines="false">${escapeHtml(t.text)}</Pre>\n`
162
164
  }
163
165
  // Single html token that starts with block-level tag
164
166
  if (t.type === 'html') {
@@ -210,7 +212,7 @@ markdown.paragraph = (/** @type {import('marked').Tokens.Paragraph} */ token) =>
210
212
  return `<${tag}>${inner}</${tag}>`
211
213
  }
212
214
  if (t.type === 'codespan') {
213
- return `<Pre lines="false">${t.text}</Pre>`
215
+ return `<Pre lines="false">${escapeHtml(t.text)}</Pre>`
214
216
  }
215
217
  if (t.type === 'image') {
216
218
  let out = `<Img src="${t.href}"`