zenn-markdown-html 0.1.155-alpha.2 → 0.1.155

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/lib/index.js CHANGED
@@ -20,6 +20,7 @@ var _mdBr = require("./utils/md-br");
20
20
  var _mdKatex = require("./utils/md-katex");
21
21
  var _mdCustomBlock = require("./utils/md-custom-block");
22
22
  var _mdLinkAttributes = require("./utils/md-link-attributes");
23
+ var _mdSourceMap = require("./utils/md-source-map");
23
24
  var _mdLinkifyToCard = require("./utils/md-linkify-to-card");
24
25
  var _mdRendererFence = require("./utils/md-renderer-fence");
25
26
  var _mdContainer = require("./utils/md-container");
@@ -57,7 +58,7 @@ const markdownToHtml = (text, options) => {
57
58
  symbol: ''
58
59
  }),
59
60
  tabIndex: false
60
- });
61
+ }).use(_mdSourceMap.mdSourceMap);
61
62
 
62
63
  // custom footnote
63
64
  md.renderer.rules.footnote_block_open = () => '<section class="footnotes">\n' + '<span class="footnotes-title">脚注</span>\n' + '<ol class="footnotes-list">\n';
package/lib/sanitizer.js CHANGED
@@ -10,44 +10,44 @@ const tags = ['a', 'aside', 'blockquote', 'br', 'circle', 'code', 'details', 'di
10
10
  const attributes = {
11
11
  a: ['aria-hidden', 'class', 'href', 'id', 'rel', 'style', 'target', 'title'],
12
12
  aside: ['class'],
13
- blockquote: [],
13
+ blockquote: ['class', 'data-line'],
14
14
  br: ['style'],
15
15
  circle: ['cx', 'cy', 'fill', 'r'],
16
- code: ['class'],
16
+ code: ['class', 'data-line'],
17
17
  details: [],
18
18
  div: ['class'],
19
19
  em: [],
20
20
  'embed-katex': ['display-mode'],
21
21
  eq: ['class'],
22
22
  eqn: [],
23
- h1: ['id'],
24
- h2: ['id'],
25
- h3: ['id'],
26
- h4: ['id'],
27
- h5: [],
28
- h6: [],
29
- hr: [],
23
+ h1: ['id', 'class', 'data-line'],
24
+ h2: ['id', 'class', 'data-line'],
25
+ h3: ['id', 'class', 'data-line'],
26
+ h4: ['id', 'class', 'data-line'],
27
+ h5: ['class', 'data-line'],
28
+ h6: ['class', 'data-line'],
29
+ hr: ['class', 'data-line'],
30
30
  iframe: ['allow', 'allowfullscreen', 'allowtransparency', 'data-content', 'frameborder', 'id', 'loading', 'sandbox', 'scrolling', 'src', 'style', 'width'],
31
31
  img: ['alt', 'class', 'height', 'loading', 'src', 'title', 'width'],
32
32
  input: ['checked', 'class', 'type'],
33
- li: ['class', 'id'],
34
- ol: ['class', 'start'],
35
- p: [],
33
+ li: ['class', 'id', 'data-line'],
34
+ ol: ['class', 'start', 'data-line'],
35
+ p: ['class', 'data-line'],
36
36
  pre: ['class'],
37
37
  s: [],
38
- section: ['class'],
38
+ section: ['class', 'data-line'],
39
39
  span: ['class', 'title'],
40
40
  strong: [],
41
41
  summary: [],
42
42
  sup: ['class'],
43
- table: [],
44
- tbody: [],
43
+ table: ['class', 'data-line'],
44
+ tbody: ['class', 'data-line'],
45
45
  td: ['style'],
46
46
  text: ['dominant-baseline', 'fill', 'font-size', 'font-weight', 'text-anchor', 'x', 'y'],
47
47
  th: ['style'],
48
- thead: [],
49
- tr: [],
50
- ul: ['class']
48
+ thead: ['class', 'data-line'],
49
+ tr: ['class', 'data-line'],
50
+ ul: ['class', 'data-line']
51
51
  };
52
52
  const sanitize = html => (0, _sanitizeHtml.default)(html, {
53
53
  allowedTags: tags,
@@ -42,12 +42,12 @@ const inlineRules = [{
42
42
  const blockRules = [{
43
43
  name: 'math_block_eqno',
44
44
  rex: /\${2}([^$]+?)\${2}\s*?\(([^)\s]+?)\)/gmy,
45
- tmpl: `<section class="${katexClassName} eqno"><eqn><embed-katex display-mode="1">$1</embed-katex></eqn><span>($2)</span></section>`,
45
+ tmpl: `<section class="${katexClassName} eqno code-line" $3><eqn><embed-katex display-mode="1">$1</embed-katex></eqn><span>($2)</span></section>`,
46
46
  tag: '$$'
47
47
  }, {
48
48
  name: 'math_block',
49
49
  rex: /\${2}([^$]+?)\${2}/gmy,
50
- tmpl: `<section class="${katexClassName}"><eqn><embed-katex display-mode="1">$1</embed-katex></eqn></section>`,
50
+ tmpl: `<section class="${katexClassName} code-line" $3><eqn><embed-katex display-mode="1">$1</embed-katex></eqn></section>`,
51
51
  tag: '$$'
52
52
  }];
53
53
  function mdKatex(md) {
@@ -115,6 +115,6 @@ function mdKatex(md) {
115
115
  }); // ! important for ```math delimiters
116
116
 
117
117
  md.renderer.rules[rule.name] = (tokens, idx) => rule.tmpl.replace(/\$2/, md.utils.escapeHtml(tokens[idx].info)) // equation number .. ?
118
- .replace(/\$1/, md.utils.escapeHtml(tokens[idx].content));
118
+ .replace(/\$1/, md.utils.escapeHtml(tokens[idx].content)).replace(/\$3/, 'data-line="' + tokens[idx].attrGet('data-line') + '"');
119
119
  }
120
120
  }
@@ -10,10 +10,11 @@ var _highlight = require("./highlight");
10
10
  function getHtml({
11
11
  content,
12
12
  className,
13
- fileName
13
+ fileName,
14
+ line
14
15
  }) {
15
16
  const escapedClass = (0, _utils.escapeHtml)(className);
16
- return `<div class="code-block-container">${fileName ? `<div class="code-block-filename-container"><span class="code-block-filename">${(0, _utils.escapeHtml)(fileName)}</span></div>` : ''}<pre class="${escapedClass}"><code class="${escapedClass}">${content}</code></pre></div>`;
17
+ return `<div class="code-block-container">${fileName ? `<div class="code-block-filename-container"><span class="code-block-filename">${(0, _utils.escapeHtml)(fileName)}</span></div>` : ''}<pre class="${escapedClass}"><code class="${escapedClass !== '' ? `${escapedClass} code-line` : 'code-line'}" ${line !== undefined ? `data-line="${line}"` : ''}>${content}</code></pre></div>`;
17
18
  }
18
19
  function getClassName({
19
20
  langName = '',
@@ -64,6 +65,7 @@ function parseInfo(str) {
64
65
  function mdRendererFence(md, options) {
65
66
  // override fence
66
67
  md.renderer.rules.fence = function (...args) {
68
+ var _tokens$idx$map;
67
69
  const [tokens, idx] = args;
68
70
  const {
69
71
  info,
@@ -85,10 +87,12 @@ function mdRendererFence(md, options) {
85
87
  hasDiff
86
88
  });
87
89
  const highlightedContent = (0, _highlight.highlight)(content, langName, hasDiff);
90
+ const fenceStart = (_tokens$idx$map = tokens[idx].map) === null || _tokens$idx$map === void 0 ? void 0 : _tokens$idx$map[0];
88
91
  return getHtml({
89
92
  content: highlightedContent,
90
93
  className,
91
- fileName
94
+ fileName,
95
+ line: fenceStart
92
96
  });
93
97
  };
94
98
  }
@@ -0,0 +1,7 @@
1
+ import MarkdownIt from "markdown-it";
2
+ /**
3
+ * Adds begin line index to the output via the 'data-line' data attribute.
4
+ *
5
+ * Ref: https://github.com/microsoft/vscode/blob/84f63bf4e54c60e40865c8c4d8002893a337fe61/extensions/markdown-language-features/src/markdownEngine.ts#L17-L40
6
+ */
7
+ export declare function mdSourceMap(md: MarkdownIt): void;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.mdSourceMap = mdSourceMap;
7
+ /**
8
+ * Adds begin line index to the output via the 'data-line' data attribute.
9
+ *
10
+ * Ref: https://github.com/microsoft/vscode/blob/84f63bf4e54c60e40865c8c4d8002893a337fe61/extensions/markdown-language-features/src/markdownEngine.ts#L17-L40
11
+ */
12
+ function mdSourceMap(md) {
13
+ // Set the attribute on every possible token.
14
+ md.core.ruler.push("source_map_data_attribute", state => {
15
+ for (const token of state.tokens) {
16
+ if (token.map && token.type !== "inline") {
17
+ token.attrSet("data-line", String(token.map[0]));
18
+ token.attrJoin("class", "code-line");
19
+ }
20
+ }
21
+ });
22
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zenn-markdown-html",
3
- "version": "0.1.155-alpha.2",
3
+ "version": "0.1.155",
4
4
  "license": "MIT",
5
5
  "description": "Convert markdown to zenn flavor html.",
6
6
  "main": "lib/index.js",
@@ -60,7 +60,7 @@
60
60
  "prismjs": "^1.29.0",
61
61
  "sanitize-html": "^2.9.0"
62
62
  },
63
- "gitHead": "577c1e248e10e2bc5a10214e01c4b7223d66a637",
63
+ "gitHead": "7c4b0fd3e18ab7df9ff92657598ebd8616cac538",
64
64
  "publishConfig": {
65
65
  "access": "public"
66
66
  }