all repos — gemini-redirect @ 2ebd1bde5e29db5a6f1db1b278a08cfcf974ff2d

content/blog/graphs/tinyparser.js (view raw)

 1const mathify = content => {
 2  let result = ''
 3  let close = null;
 4  let symbol = null;
 5  Array.from(content).forEach(c => {
 6      if (symbol !== null) {
 7          if (c === ' ') {
 8              if (symbol === 'rightarrow') {
 9                  result += '→'
10              } else if (symbol === 'geq') {
11                  result += '≥'
12              } else {
13                  result += '?'
14              }
15              symbol = null;
16              result += ' '
17          } else {
18              symbol += c
19          }
20      } else if (c == '$') {
21          symbol = ''
22      } else if (c === '*') {
23          result += '×'
24      } else if (c === '^') {
25          result += '<sup>'
26          close = '</sup>'
27      } else if (c === '_') {
28          result += '<sub>';
29          close = '</sub>';
30      } else if (c === ' ' || c == '\n') {
31          if (close !== null) {
32              result += close
33              close = null
34          }
35          result += '&nbsp;'
36      } else {
37          result += c
38      }
39  })
40  if (close !== null) {
41      result += close;
42  }
43  return `<em class="math">${result.trim()}</em>`
44}
45
46const parse_maths = html => html.replaceAll(/\(\(.+?\)\)/g, match =>
47  mathify(match.slice(2, -2))
48);
49
50Array.from(document.getElementsByClassName('matrix')).forEach(matrix => {
51  let result = '<table>'
52  matrix.innerHTML.trim().split('\\').forEach(row => {
53      result += '<tr>'
54      row.trim().split("'").forEach(elem => {
55          result += `<td>${mathify(elem.trim())}</td>`
56      })
57      result += '</tr>'
58  })
59  matrix.innerHTML = result
60})
61
62Array.from(document.getElementsByTagName('p')).forEach(paragraph => {
63  paragraph.innerHTML = parse_maths(paragraph.innerHTML)
64})