Spaces:
Running

File size: 5,776 Bytes
1d919a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Markdown for Champ's answers, built as DOM nodes (never innerHTML), so text
// from the model or a service cannot inject markup. Covers what chat answers
// use: headings, paragraphs, lists (with task boxes), quotes, tables, rules,
// fenced code with a copy button, and inline code, bold, italic and links.

function el(tag, cls, text) {
  const e = document.createElement(tag);
  if (cls) e.className = cls;
  if (text != null) e.textContent = text;
  return e;
}

// Inline: `code`, **bold**, *italic* / _italic_, [text](http…), bare http(s) links.
const INLINE = /(`+)([\s\S]+?)\1|\*\*([^*\n]+)\*\*|__([^_\n]+)__|\*([^*\n]+)\*|(?<![\w])_([^_\n]+)_(?![\w])|\[([^\]\n]+)\]\((https?:\/\/[^\s)]+)\)|(https?:\/\/[^\s<>()]+[^\s<>().,;:!?'"])/g;

export function inline(parent, text) {
  const s = String(text || "");
  let at = 0;
  for (const m of s.matchAll(INLINE)) {
    if (m.index > at) parent.append(s.slice(at, m.index));
    if (m[2] != null) parent.append(el("code", "", m[2]));
    else if (m[3] != null || m[4] != null) { const b = el("strong"); inline(b, m[3] || m[4]); parent.append(b); }
    else if (m[5] != null || m[6] != null) { const i = el("em"); inline(i, m[5] || m[6]); parent.append(i); }
    else {
      const label = m[7] != null ? m[7] : m[9];
      const a = el("a", /^\d{1,2}$/.test(label) ? "cite" : "", label);
      a.href = m[8] || m[9];
      a.target = "_blank";
      a.rel = "noopener noreferrer";
      parent.append(a);
    }
    at = m.index + m[0].length;
  }
  if (at < s.length) parent.append(s.slice(at));
}

function codeBlock(lang, code) {
  const wrap = el("div", "code");
  const bar = el("div", "code-bar");
  bar.append(el("span", "", lang || "code"));
  const copy = el("button", "copy", "Copy");
  copy.type = "button";
  copy.onclick = () => copyText(code, copy);
  bar.append(copy);
  const pre = el("pre");
  pre.append(el("code", "", code));
  wrap.append(bar, pre);
  return wrap;
}

export function copyText(text, button) {
  const done = () => {
    if (!button) return;
    const was = button.textContent;
    button.textContent = "Copied";
    setTimeout(() => { button.textContent = was; }, 1200);
  };
  if (navigator.clipboard && navigator.clipboard.writeText) {
    navigator.clipboard.writeText(text).then(done, done);
    return;
  }
  const ta = el("textarea");
  ta.value = text;
  document.body.append(ta);
  ta.select();
  try { document.execCommand("copy"); } catch { /* nothing to copy with */ }
  ta.remove();
  done();
}

function cells(line) {
  return line.trim().replace(/^\|/, "").replace(/\|$/, "").split("|").map((c) => c.trim());
}

export function renderMarkdown(md, cls) {
  const root = el("div", cls || "md");
  const lines = String(md || "").replace(/\r\n/g, "\n").split("\n");
  let para = [];
  const flush = () => {
    if (!para.length) return;
    const p = el("p");
    para.forEach((l, i) => { if (i) p.append(el("br")); inline(p, l); });
    root.append(p);
    para = [];
  };
  for (let i = 0; i < lines.length; i++) {
    const raw = lines[i];
    const t = raw.trim();
    let m;
    if ((m = /^(`{3,}|~{3,})\s*([\w+#.-]*)/.exec(t))) {
      flush();
      const fence = m[1];
      const code = [];
      for (i++; i < lines.length && !lines[i].trim().startsWith(fence); i++) code.push(lines[i]);
      root.append(codeBlock(m[2], code.join("\n")));
      continue;
    }
    if (!t) { flush(); continue; }
    if (/^(-{3,}|\*{3,}|_{3,})$/.test(t)) { flush(); root.append(el("hr")); continue; }
    if ((m = /^(#{1,6})\s+(.*?)\s*#*$/.exec(t))) {
      flush();
      const h = el("h" + Math.min(6, m[1].length + 2));
      inline(h, m[2]);
      root.append(h);
      continue;
    }
    if (t.startsWith("|") && i + 1 < lines.length && /^\|?\s*:?-{2,}/.test(lines[i + 1].trim())) {
      flush();
      const table = el("table");
      const tr = el("tr");
      for (const c of cells(t)) { const th = el("th"); inline(th, c); tr.append(th); }
      const thead = el("thead");
      thead.append(tr);
      table.append(thead);
      const tbody = el("tbody");
      for (i += 2; i < lines.length && lines[i].trim().startsWith("|"); i++) {
        const row = el("tr");
        for (const c of cells(lines[i])) { const td = el("td"); inline(td, c); row.append(td); }
        tbody.append(row);
      }
      i--;
      table.append(tbody);
      const scroll = el("div", "table");
      scroll.append(table);
      root.append(scroll);
      continue;
    }
    if (/^(?:[-*+]|\d+[.)])\s+/.test(t)) {
      flush();
      const ordered = /^\d/.test(t);
      const list = el(ordered ? "ol" : "ul");
      for (; i < lines.length; i++) {
        const lt = lines[i].trim();
        const item = /^(?:[-*+]|\d+[.)])\s+(.*)$/.exec(lt);
        if (!item) {
          if (lt && list.lastChild && /^\s{2,}/.test(lines[i])) { list.lastChild.append(" "); inline(list.lastChild, lt); continue; }
          break;
        }
        const li = el("li");
        const task = /^\[([ xX])\]\s+(.*)$/.exec(item[1]);
        if (task) {
          const box = el("input");
          box.type = "checkbox";
          box.disabled = true;
          box.checked = task[1] !== " ";
          li.className = "task-item";
          li.append(box, " ");
          inline(li, task[2]);
        } else inline(li, item[1]);
        list.append(li);
      }
      i--;
      root.append(list);
      continue;
    }
    if (t.startsWith(">")) {
      flush();
      const q = [];
      for (; i < lines.length && lines[i].trim().startsWith(">"); i++) q.push(lines[i].trim().replace(/^>\s?/, ""));
      i--;
      const bq = el("blockquote");
      bq.append(renderMarkdown(q.join("\n"), "md-inner"));
      root.append(bq);
      continue;
    }
    para.push(t);
  }
  flush();
  return root;
}