@chromamark/renderer 0.2.0 → 0.2.2

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.
@@ -5929,13 +5929,52 @@ function makeRule2(enabled) {
5929
5929
  if (silent) return true;
5930
5930
  let nextLine = startLine;
5931
5931
  let autoClosed = false;
5932
+ let fenceCh = 0;
5933
+ let fenceLen = 0;
5932
5934
  for (; ; ) {
5933
5935
  nextLine++;
5934
5936
  if (nextLine >= endLine) break;
5935
5937
  const lstart = state.bMarks[nextLine] + state.tShift[nextLine];
5936
5938
  const lmax = state.eMarks[nextLine];
5937
- if (state.src.charCodeAt(lstart) !== MARKER) continue;
5938
- if (state.sCount[nextLine] - state.blkIndent >= 4) continue;
5939
+ const indent = state.sCount[nextLine] - state.blkIndent;
5940
+ if (fenceCh) {
5941
+ if (indent < 4 && state.src.charCodeAt(lstart) === fenceCh) {
5942
+ let q = lstart;
5943
+ while (q < lmax && state.src.charCodeAt(q) === fenceCh) q++;
5944
+ if (q - lstart >= fenceLen) {
5945
+ let r = q;
5946
+ while (r < lmax && (state.src.charCodeAt(r) === 32 || state.src.charCodeAt(r) === 9)) r++;
5947
+ if (r >= lmax) {
5948
+ fenceCh = 0;
5949
+ fenceLen = 0;
5950
+ }
5951
+ }
5952
+ }
5953
+ continue;
5954
+ }
5955
+ const openCh = state.src.charCodeAt(lstart);
5956
+ if (indent < 4 && (openCh === 96 || openCh === 126)) {
5957
+ let q = lstart;
5958
+ while (q < lmax && state.src.charCodeAt(q) === openCh) q++;
5959
+ if (q - lstart >= 3) {
5960
+ let ok = true;
5961
+ if (openCh === 96) {
5962
+ for (let r = q; r < lmax; r++) {
5963
+ if (state.src.charCodeAt(r) === 96) {
5964
+ ok = false;
5965
+ break;
5966
+ }
5967
+ }
5968
+ }
5969
+ if (ok) {
5970
+ fenceCh = openCh;
5971
+ fenceLen = q - lstart;
5972
+ continue;
5973
+ }
5974
+ }
5975
+ }
5976
+ if (openCh !== MARKER) continue;
5977
+ if (indent >= 4) continue;
5939
5978
  const closeLen = fenceLength(state.src, lstart, lmax);
5940
5979
  if (closeLen < openLen) continue;
5941
5980
  let p = lstart + closeLen;
@@ -5981,6 +6020,15 @@ function containerPlugin(md, enabled) {
5981
6020
  alt: ["paragraph", "reference", "blockquote", "list"]
5982
6021
  });
5983
6022
  const esc = md.utils.escapeHtml;
6023
+ const renderInlineSafe = (text2) => {
6024
+ const prevHtml = md.options.html;
6025
+ md.options.html = false;
6026
+ try {
6027
+ return md.renderInline(text2);
6028
+ } finally {
6029
+ md.options.html = prevHtml;
6030
+ }
6031
+ };
5984
6032
  function decorate(meta) {
5985
6033
  const custom = meta.color ? " cm-custom" : "";
5986
6034
  const style = meta.color ? ` style="--fg:${esc(meta.color)}"` : "";
@@ -5992,26 +6040,38 @@ function containerPlugin(md, enabled) {
5992
6040
  const { custom, style, tone } = decorate(meta);
5993
6041
  if (meta.structure === "details") {
5994
6042
  const open = meta.open ? " open" : "";
5995
- return `<details class="cm-details${custom}"${tone}${style}${open}><summary>${esc(meta.summary)}</summary><div class="cm-body">`;
6043
+ return `<details class="cm-details${custom}"${tone}${style}${open}><summary>${renderInlineSafe(meta.summary)}</summary><div class="cm-body">`;
5996
6044
  }
5997
6045
  let html = `<div class="cm-block${custom}"${tone}${style}>`;
5998
- if (meta.title) html += `<div class="cm-title">${esc(meta.title)}</div>`;
6046
+ if (meta.title) html += `<div class="cm-title">${renderInlineSafe(meta.title)}</div>`;
5999
6047
  return html + '<div class="cm-body">';
6000
6048
  };
6001
6049
  md.renderer.rules.cm_container_close = (tokens, idx) => tokens[idx].meta.structure === "details" ? "</div></details>" : "</div></div>";
6002
6050
  md.renderer.rules.cm_fields = (tokens, idx) => {
6003
- const prevHtml = md.options.html;
6004
- md.options.html = false;
6005
- try {
6006
- let html = '<dl class="cm-fields">';
6007
- for (const [key, value] of tokens[idx].meta.rows) {
6008
- html += `<dt>${esc(key)}</dt><dd>${md.renderInline(value)}</dd>`;
6009
- }
6010
- return html + "</dl>";
6011
- } finally {
6012
- md.options.html = prevHtml;
6051
+ let html = '<dl class="cm-fields">';
6052
+ for (const [key, value] of tokens[idx].meta.rows) {
6053
+ html += `<dt>${esc(key)}</dt><dd>${renderInlineSafe(value)}</dd>`;
6013
6054
  }
6055
+ return html + "</dl>";
6014
6056
  };
6057
+ md.core.ruler.push("cm_sanitize_bodies", (state) => {
6058
+ let depth = 0;
6059
+ for (const token of state.tokens) {
6060
+ if (token.type === "cm_container_open") {
6061
+ depth++;
6062
+ } else if (token.type === "cm_container_close") {
6063
+ depth--;
6064
+ } else if (depth > 0) {
6065
+ if (token.type === "html_block") {
6066
+ token.content = esc(token.content);
6067
+ } else if (token.type === "inline" && token.children) {
6068
+ for (const child of token.children) {
6069
+ if (child.type === "html_inline") child.content = esc(child.content);
6070
+ }
6071
+ }
6072
+ }
6073
+ }
6074
+ });
6015
6075
  }
6016
6076
 
6017
6077
  // src/index.js
@@ -6078,16 +6138,25 @@ function injectTheme(doc) {
6078
6138
  (d.head || d.documentElement).appendChild(style);
6079
6139
  }
6080
6140
  function dedent(text2) {
6081
- const lines = text2.replace(/\t/g, " ").replace(/\r/g, "").split("\n");
6141
+ const lines = text2.replace(/\r/g, "").split("\n");
6082
6142
  while (lines.length && lines[0].trim() === "") lines.shift();
6083
6143
  while (lines.length && lines[lines.length - 1].trim() === "") lines.pop();
6084
- let min = Infinity;
6144
+ let prefix = null;
6085
6145
  for (const line of lines) {
6086
6146
  if (!line.trim()) continue;
6087
- min = Math.min(min, line.match(/^ */)[0].length);
6147
+ const lead = line.match(/^[ \t]*/)[0];
6148
+ if (prefix === null) {
6149
+ prefix = lead;
6150
+ continue;
6151
+ }
6152
+ let i = 0;
6153
+ const lim = Math.min(prefix.length, lead.length);
6154
+ while (i < lim && prefix[i] === lead[i]) i++;
6155
+ prefix = prefix.slice(0, i);
6156
+ if (prefix === "") break;
6088
6157
  }
6089
- if (!Number.isFinite(min)) min = 0;
6090
- return lines.map((line) => line.slice(min)).join("\n");
6158
+ const cut = prefix ? prefix.length : 0;
6159
+ return lines.map((line) => line.slice(cut)).join("\n");
6091
6160
  }
6092
6161
  function resolve(target) {
6093
6162
  return typeof target === "string" ? document.querySelector(target) : target;