gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit69b0a0ce69b0a0ceident#34: access: the pick buttons use Style.pickCard instead of class pickmre69b0a0ce/plugins/markdown/mdsyntax.hl

41.0 KB

  1. // hl:markdown — THE SYNTAX: Markdown text → plain data → Markdown text (ticket #111).
  2. // One parser for every app. Merged from tickets.worldapi.org/markdown.hl (the base:
  3. // safe links, emphasis by lookahead, autolinks), gitoria.worldapi.org/markdown.hl
  4. // (quotes, tables, rules) and components.hybriel's md-editor.hl (images, the
  5. // Markdown writer), plus what LLM answers need: nested lists, task items, code
  6. // fences with a language, ~~strike~~, and text that is still arriving.
  7. //
  8. // THE DATA. parse(text) answers a document `{ blocks, tail }`; every block carries
  9. // `type` and `gap` (the blank lines written before it):
  10. // p { spans }
  11. // heading { level (1-6), spans, trail (a closing ` ##`, usually '') }
  12. // code { code (every line + NL), lang (first word of the info string), info,
  13. // fence ('```', '~~~~' …; '' = an indented block), closed (false while
  14. // a fence is still open — streaming) }
  15. // list { ordered, start, marker ('-' '*' '+', or '.' ')' after a number),
  16. // indent, tight, items: [{ gap, num, task (null | true | false), cont
  17. // (the indent of its continuation lines), blocks }] }
  18. // quote { blocks }
  19. // table { align (['' | 'left' | 'center' | 'right']), head: { cells, src },
  20. // sep (the |---| line), rows: [{ cells, src }] }; a cell is { spans }
  21. // hr { src ('---', '***' …) }
  22. // `tail` is the number of newlines after the last block.
  23. // A span: { text, strong, em, strike, code, href, image, br, src }. `href` is ''
  24. // or a link that safeHref let through; `image` spans are links to the picture
  25. // (the View never loads one); `br` is a line break inside a block (text = NL).
  26. // `src` is the exact source the span came from — see serialize.
  27. //
  28. // SAFE BY CONSTRUCTION: nothing here builds HTML. Raw HTML in the text stays text
  29. // (`<script>` is ten characters). A link or image whose target safeHref refuses
  30. // (`javascript:`, `data:`, relative paths, spaces, quotes, control characters) is
  31. // kept as the plain text it was written as.
  32. //
  33. // STREAMING-SAFE: any prefix of a document parses. An open fence is a code block
  34. // with `closed = false`; a half list is a list; an emphasis run with no closer yet
  35. // is literal text; a table head without its |---| line is a paragraph. Nothing
  36. // here throws on input — every index is bounds-checked.
  37. //
  38. // ROUND TRIP: serialize(parse(t)) == t for normal documents. Each block kind keeps
  39. // its markers (bullet, fence, number, rule), blank lines are counted, and a
  40. // block's inline text is written from the spans' `src` when re-reading that
  41. // source gives the same spans — so an edited span (its text or flags changed, or
  42. // a span built by hand without `src`) makes that block's text canonical Markdown,
  43. // and every untouched block comes back byte for byte. Normalised, not kept: CR
  44. // characters, tabs in indentation, spaces on blank lines, a heading's or list
  45. // marker's extra spaces, lazy (unindented) continuation lines of a list item or
  46. // quote, `>` without its space.
  47. static NL = "
  48. "
  49. static BS = 'x\y'.slice(1, 2)
  50. // ---- characters -------------------------------------------------------------------
  51. static isSpaceC = (c) => { return c == 32 || c == 9 || c == 10 || c == 13 }
  52. static isAlnumC = (c) => { return (c >= 48 && c <= 57) || (c >= 65 && c <= 90) || (c >= 97 && c <= 122) || c > 127 }
  53. static isPunctC = (c) => { return (c >= 33 && c <= 47) || (c >= 58 && c <= 64) || (c >= 91 && c <= 96) || (c >= 123 && c <= 126) }
  54. static rep = (s, n) => {
  55. let out = ''
  56. let i = 0
  57. while (i < n) {
  58. out = out + s
  59. i = i + 1
  60. }
  61. return out
  62. }
  63. // ---- links --------------------------------------------------------------------------
  64. // the target of a link, or null when it is not one we let through: http(s), mailto, or
  65. // a SAME-SITE relative reference (`/path`, `./x`, `../x`, `x.html`, `#anchor`, `?q`) —
  66. // no scheme at all, and not `//host` (another site). Never spaces, control
  67. // characters, quotes, < > ` or backslash
  68. static safeHref = (u) => {
  69. if (u == null) { return null }
  70. let t = ('' + u).trim()
  71. if (t == '' || t.length > 2000) { return null }
  72. let i = 0
  73. while (i < t.length) {
  74. let c = t.charCodeAt(i)
  75. if (c <= 32 || c == 127 || c == 34 || c == 39 || c == 60 || c == 62 || c == 96 || c == 92) { return null }
  76. i = i + 1
  77. }
  78. let low = t.toLowerCase()
  79. if (low.startsWith('https://') && t.length > 8) { return t }
  80. if (low.startsWith('http://') && t.length > 7) { return t }
  81. if (low.startsWith('mailto:') && t.length > 7) { return t }
  82. // relative: not `//host`, and no ':' anywhere — a ':' is a scheme, or could become
  83. // one (`java&#115;cript:` is a ':' after a '#'); strict beats clever here
  84. if (t.startsWith('//') || t.includes(':')) { return null }
  85. return t
  86. }
  87. static isUrlStart = (s, i) => {
  88. let low = s.slice(i, i + 8).toLowerCase()
  89. return low.startsWith('https://') || low.startsWith('http://')
  90. }
  91. // where a bare URL starting at i ends: up to whitespace or < > " `, minus trailing
  92. // punctuation (and a closing parenthesis that has no opening one inside the URL)
  93. static isUrlStop = (c) => { return isSpaceC(c) || c == 60 || c == 62 || c == 34 || c == 96 }
  94. static urlEnd = (s, i) => {
  95. let end = i
  96. while (end < s.length && !isUrlStop(s.charCodeAt(end))) { end = end + 1 }
  97. let trimming = true
  98. while (trimming && end > i) {
  99. let c = s.charCodeAt(end - 1)
  100. if (c == 46 || c == 44 || c == 59 || c == 58 || c == 33 || c == 63 || c == 39 || c == 42 || c == 95 || c == 126) {
  101. end = end - 1
  102. } else if (c == 41) {
  103. let opens = 0
  104. let closes = 0
  105. let k = i
  106. while (k < end) {
  107. if (s.charCodeAt(k) == 40) { opens = opens + 1 }
  108. if (s.charCodeAt(k) == 41) { closes = closes + 1 }
  109. k = k + 1
  110. }
  111. if (closes > opens) { end = end - 1 } else { trimming = false }
  112. } else {
  113. trimming = false
  114. }
  115. }
  116. return end
  117. }
  118. // `[label](target)` at i → { label, href (null = not allowed), end } or null (not a link)
  119. static linkAt = (s, i) => {
  120. let j = i + 1
  121. let found = -1
  122. let depth = 0
  123. while (j < s.length && found < 0) {
  124. let c = s.charCodeAt(j)
  125. if (c == 92) {
  126. j = j + 1
  127. } else if (c == 96) {
  128. // a code span inside the label is skipped whole
  129. let k = j
  130. while (k < s.length && s.charCodeAt(k) == 96) { k = k + 1 }
  131. let close = runAt(s, k, 96, k - j, false)
  132. if (close >= 0) { j = close + (k - j) - 1 } else { j = k - 1 }
  133. } else if (c == 91) {
  134. depth = depth + 1
  135. } else if (c == 93) {
  136. if (depth == 0) { found = j } else { depth = depth - 1 }
  137. }
  138. j = j + 1
  139. }
  140. if (found < 0 || found == i + 1) { return null }
  141. if (found + 1 >= s.length || s.charCodeAt(found + 1) != 40) { return null }
  142. // the target ends at the `)` that balances its `(` — `(https://x.org/a_(b))`
  143. let k = found + 2
  144. let depth2 = 0
  145. let endAt = -1
  146. while (k < s.length && endAt < 0) {
  147. let c2 = s.charCodeAt(k)
  148. if (c2 == 92) {
  149. k = k + 1
  150. } else if (c2 == 40) {
  151. depth2 = depth2 + 1
  152. } else if (c2 == 41) {
  153. if (depth2 == 0) { endAt = k } else { depth2 = depth2 - 1 }
  154. } else if (c2 == 10) {
  155. return null
  156. }
  157. k = k + 1
  158. }
  159. if (endAt < 0) { return null }
  160. k = endAt
  161. let target = s.slice(found + 2, k).trim()
  162. if (target.startsWith('<') && target.endsWith('>')) { target = target.slice(1, target.length - 1) }
  163. // an optional title after a space is dropped: [x](url "title")
  164. let sp = target.indexOf(' ')
  165. if (sp > 0) { target = target.slice(0, sp) }
  166. return { label = s.slice(i + 1, found) href = safeHref(target) end = k + 1 }
  167. }
  168. // ---- spans ----------------------------------------------------------------------------
  169. static span = (t, f) => {
  170. return { text = t strong = f.strong em = f.em strike = f.strike code = false href = f.href image = false br = false src = '' }
  171. }
  172. // the index of a run of EXACTLY n characters c at or after `at`, or -1. `esc`: a
  173. // character after a backslash does not count (inside `code` a backslash is just one)
  174. static runAt = (s, at, c, n, esc) => {
  175. let i = at
  176. while (i < s.length) {
  177. if (esc && s.charCodeAt(i) == 92) {
  178. i = i + 2
  179. } else if (s.charCodeAt(i) == c) {
  180. let k = i
  181. while (k < s.length && s.charCodeAt(k) == c) { k = k + 1 }
  182. if (k - i == n) { return i }
  183. i = k
  184. } else {
  185. i = i + 1
  186. }
  187. }
  188. return -1
  189. }
  190. // is there a CLOSING delimiter run (exactly n of c, not after a space; `_` not before
  191. // a letter or digit) after `at`, before the end of the line group?
  192. static hasCloser = (s, at, c, n) => {
  193. let i = at
  194. while (i < s.length) {
  195. let r = runAt(s, i, c, n, true)
  196. if (r < 0) { return false }
  197. let prev = r > 0 ? s.charCodeAt(r - 1) : 32
  198. let next = r + n < s.length ? s.charCodeAt(r + n) : 32
  199. if (r > at && !isSpaceC(prev) && (c != 95 || !isAlnumC(next))) { return true }
  200. i = r + n
  201. }
  202. return false
  203. }
  204. static trimEndSp = (t) => {
  205. let e = t.length
  206. while (e > 0 && (t.charCodeAt(e - 1) == 32 || t.charCodeAt(e - 1) == 9)) { e = e - 1 }
  207. return t.slice(0, e)
  208. }
  209. // inline Markdown → spans. `base` holds the flags of an enclosing link (its label is
  210. // read by the same function). Every character of `src` lands in exactly one span's
  211. // `src`, so the spans' src joined IS the source.
  212. static parseInline = (source, base) => {
  213. let out = []
  214. if (source == null) { return out }
  215. let s = '' + source
  216. let n = s.length
  217. let f = { strong = base.strong em = base.em strike = base.strike href = base.href }
  218. let strongC = 0
  219. let emC = 0
  220. let buf = ''
  221. let acc = ''
  222. let i = 0
  223. // the leading spaces of the text are source, not text
  224. while (i < n && (s.charCodeAt(i) == 32 || s.charCodeAt(i) == 9)) {
  225. acc = acc + s[i]
  226. i = i + 1
  227. }
  228. while (i < n) {
  229. let c = s.charCodeAt(i)
  230. if (c == 10 || (c == 92 && i + 1 < n && s.charCodeAt(i + 1) == 10)) {
  231. // a line break: the text before it loses its trailing spaces, the next line its
  232. // leading ones; a backslash before it is a hard break's marker
  233. if (buf != '') {
  234. let sp = span(trimEndSp(buf), f)
  235. sp.src = acc
  236. out[] = sp
  237. } else if (acc != '' && out.length > 0) {
  238. out[out.length - 1].src = out[out.length - 1].src + acc
  239. } else if (acc != '') {
  240. let sp0 = span('', f)
  241. sp0.src = acc
  242. out[] = sp0
  243. }
  244. buf = ''
  245. acc = c == 92 ? BS + NL : NL
  246. i = i + (c == 92 ? 2 : 1)
  247. while (i < n && (s.charCodeAt(i) == 32 || s.charCodeAt(i) == 9)) {
  248. acc = acc + s[i]
  249. i = i + 1
  250. }
  251. let b = span(NL, f)
  252. b.br = true
  253. b.src = acc
  254. out[] = b
  255. acc = ''
  256. } else if (c == 92 && i + 1 < n && isPunctC(s.charCodeAt(i + 1))) {
  257. // a backslash escape: the next punctuation character as itself
  258. buf = buf + s[i + 1]
  259. acc = acc + s.slice(i, i + 2)
  260. i = i + 2
  261. } else if (c == 96) {
  262. // `code`: a run of backticks up to the same run
  263. let k = i
  264. while (k < n && s.charCodeAt(k) == 96) { k = k + 1 }
  265. let close = runAt(s, k, 96, k - i, false)
  266. if (close < 0) {
  267. buf = buf + s.slice(i, k)
  268. acc = acc + s.slice(i, k)
  269. i = k
  270. } else {
  271. if (buf != '') {
  272. let sp1 = span(buf, f)
  273. sp1.src = acc
  274. out[] = sp1
  275. acc = ''
  276. }
  277. buf = ''
  278. let code = s.slice(k, close).replaceAll(NL, ' ')
  279. if (code.length > 2 && code.startsWith(' ') && code.endsWith(' ') && code.trim() != '') { code = code.slice(1, code.length - 1) }
  280. let cs = span(code, f)
  281. cs.code = true
  282. cs.src = acc + s.slice(i, close + (k - i))
  283. acc = ''
  284. out[] = cs
  285. i = close + (k - i)
  286. }
  287. } else if (f.href == '' && (c == 91 || (c == 33 && i + 1 < n && s.charCodeAt(i + 1) == 91))) {
  288. // [label](url) and ![alt](src)
  289. let img = c == 33
  290. let l = linkAt(s, img ? i + 1 : i)
  291. if (l == null) {
  292. buf = buf + s[i]
  293. acc = acc + s[i]
  294. i = i + 1
  295. } else if (l.href == null) {
  296. // a target we do not let through: the whole thing is text
  297. buf = buf + s.slice(i, l.end)
  298. acc = acc + s.slice(i, l.end)
  299. i = l.end
  300. } else {
  301. if (buf != '') {
  302. let sp2 = span(buf, f)
  303. sp2.src = acc
  304. out[] = sp2
  305. acc = ''
  306. }
  307. buf = ''
  308. let open = img ? '![' : '['
  309. let tailSrc = s.slice(i + open.length + l.label.length, l.end)
  310. if (img) {
  311. let im = span(l.label, f)
  312. im.image = true
  313. im.href = l.href
  314. im.src = acc + s.slice(i, l.end)
  315. out[] = im
  316. } else {
  317. let inner = parseInline(l.label, { strong = f.strong em = f.em strike = f.strike href = l.href })
  318. if (inner.length == 0) {
  319. let e0 = span('', f)
  320. e0.href = l.href
  321. inner[] = e0
  322. }
  323. inner[0].src = acc + open + inner[0].src
  324. inner[inner.length - 1].src = inner[inner.length - 1].src + tailSrc
  325. for (x of inner) { out[] = x }
  326. }
  327. acc = ''
  328. i = l.end
  329. }
  330. } else if (f.href == '' && c == 60 && i + 1 < n && (isUrlStart(s, i + 1) || s.slice(i + 1, i + 8).toLowerCase() == 'mailto:')) {
  331. // <https://…>
  332. let e = s.indexOf('>', i + 1)
  333. let url = e > 0 ? s.slice(i + 1, e) : ''
  334. let h = safeHref(url)
  335. if (e < 0 || h == null) {
  336. buf = buf + '<'
  337. acc = acc + '<'
  338. i = i + 1
  339. } else {
  340. if (buf != '') {
  341. let sp3 = span(buf, f)
  342. sp3.src = acc
  343. out[] = sp3
  344. acc = ''
  345. }
  346. buf = ''
  347. let al = span(url, f)
  348. al.href = h
  349. al.src = acc + s.slice(i, e + 1)
  350. acc = ''
  351. out[] = al
  352. i = e + 1
  353. }
  354. } else if (f.href == '' && (c == 104 || c == 72) && (i == 0 || !isAlnumC(s.charCodeAt(i - 1))) && isUrlStart(s, i)) {
  355. // a bare http(s):// URL
  356. let e2 = urlEnd(s, i)
  357. let url2 = s.slice(i, e2)
  358. let h2 = safeHref(url2)
  359. if (h2 == null) {
  360. buf = buf + s[i]
  361. acc = acc + s[i]
  362. i = i + 1
  363. } else {
  364. if (buf != '') {
  365. let sp4 = span(buf, f)
  366. sp4.src = acc
  367. out[] = sp4
  368. acc = ''
  369. }
  370. buf = ''
  371. let bl = span(url2, f)
  372. bl.href = h2
  373. bl.src = acc + url2
  374. acc = ''
  375. out[] = bl
  376. i = e2
  377. }
  378. } else if (c == 42 || c == 95 || c == 126) {
  379. // * and _: emphasis (1), strong (2), both (3); ~~: strike
  380. let k = i
  381. while (k < n && s.charCodeAt(k) == c) { k = k + 1 }
  382. let run = k - i
  383. let prev = i > 0 ? s.charCodeAt(i - 1) : 32
  384. let next = k < n ? s.charCodeAt(k) : 32
  385. let canOpen = !isSpaceC(next) && (c != 95 || !isAlnumC(prev))
  386. let canClose = !isSpaceC(prev) && (c != 95 || !isAlnumC(next))
  387. let closes = false
  388. let opens = false
  389. if (c == 126) {
  390. closes = run == 2 && canClose && f.strike
  391. opens = !closes && run == 2 && canOpen && !f.strike && hasCloser(s, k, c, run)
  392. } else {
  393. let wantStrong = run == 2 || run == 3
  394. let wantEm = run == 1 || run == 3
  395. closes = canClose && run <= 3 && (!wantStrong || (f.strong && strongC == c)) && (!wantEm || (f.em && emC == c))
  396. opens = !closes && canOpen && run <= 3 && (!wantStrong || !f.strong) && (!wantEm || !f.em) && hasCloser(s, k, c, run)
  397. }
  398. if (closes || opens) {
  399. if (buf != '') {
  400. let sp5 = span(buf, f)
  401. sp5.src = acc
  402. out[] = sp5
  403. acc = ''
  404. }
  405. buf = ''
  406. acc = acc + s.slice(i, k)
  407. if (c == 126) {
  408. f.strike = opens
  409. } else {
  410. if (run == 2 || run == 3) {
  411. f.strong = opens
  412. strongC = c
  413. }
  414. if (run == 1 || run == 3) {
  415. f.em = opens
  416. emC = c
  417. }
  418. }
  419. } else {
  420. buf = buf + s.slice(i, k)
  421. acc = acc + s.slice(i, k)
  422. }
  423. i = k
  424. } else {
  425. buf = buf + s[i]
  426. acc = acc + s[i]
  427. i = i + 1
  428. }
  429. }
  430. if (buf != '') {
  431. let sp6 = span(trimEndSp(buf), f)
  432. sp6.src = acc
  433. out[] = sp6
  434. } else if (acc != '') {
  435. if (out.length > 0) {
  436. out[out.length - 1].src = out[out.length - 1].src + acc
  437. } else {
  438. let sp7 = span('', f)
  439. sp7.src = acc
  440. out[] = sp7
  441. }
  442. }
  443. return out
  444. }
  445. static plain = { strong = false em = false strike = false href = '' }
  446. // the spans of an inline text, with the View's flags: exactly one of isBr, isCode,
  447. // isImage, isLink, isStrongEm, isStrong, isEm, isStrike, isText is true
  448. static spansOf = (t) => {
  449. let out = []
  450. for (s of parseInline(t, plain)) {
  451. s.isBr = s.br
  452. s.isCode = !s.br && s.code
  453. s.isImage = !s.br && !s.code && s.image
  454. s.isLink = !s.br && !s.code && !s.image && s.href != ''
  455. let fmt = !s.br && !s.code && !s.image && s.href == ''
  456. s.isStrongEm = fmt && s.strong && s.em
  457. s.isStrong = fmt && s.strong && !s.em
  458. s.isEm = fmt && s.em && !s.strong
  459. s.isStrike = fmt && s.strike && !s.strong && !s.em
  460. s.isText = fmt && !s.strong && !s.em && !s.strike
  461. out[] = s
  462. }
  463. return out
  464. }
  465. // ---- blocks ---------------------------------------------------------------------------
  466. static isBlank = (line) => { return line.trim() == '' }
  467. static indentOf = (line) => {
  468. let col = 0
  469. let i = 0
  470. while (i < line.length && (line.charCodeAt(i) == 32 || line.charCodeAt(i) == 9)) {
  471. col = line.charCodeAt(i) == 9 ? col + 4 - (col % 4) : col + 1
  472. i = i + 1
  473. }
  474. return col
  475. }
  476. // the line without its first n columns of indentation
  477. static stripCols = (line, n) => {
  478. let col = 0
  479. let i = 0
  480. while (i < line.length && col < n && (line.charCodeAt(i) == 32 || line.charCodeAt(i) == 9)) {
  481. col = line.charCodeAt(i) == 9 ? col + 4 - (col % 4) : col + 1
  482. i = i + 1
  483. }
  484. return line.slice(i)
  485. }
  486. static trimStartSp = (t) => {
  487. let i = 0
  488. while (i < t.length && (t.charCodeAt(i) == 32 || t.charCodeAt(i) == 9)) { i = i + 1 }
  489. return t.slice(i)
  490. }
  491. // a heading line (≤ 3 spaces in, 1–6 '#', then a space or the end) → { level, text, trail }
  492. static headingOf = (line) => {
  493. if (indentOf(line) > 3) { return null }
  494. let t = trimEndSp(trimStartSp(line))
  495. let k = 0
  496. while (k < t.length && t.charCodeAt(k) == 35) { k = k + 1 }
  497. if (k == 0 || k > 6) { return null }
  498. if (k < t.length && t.charCodeAt(k) != 32 && t.charCodeAt(k) != 9) { return null }
  499. let body = trimStartSp(t.slice(k))
  500. let trail = ''
  501. // a closing run of '#' after a space is the heading's, not its text's
  502. let e = body.length
  503. while (e > 0 && body.charCodeAt(e - 1) == 35) { e = e - 1 }
  504. if (e < body.length && (e == 0 || body.charCodeAt(e - 1) == 32 || body.charCodeAt(e - 1) == 9)) {
  505. let e2 = e
  506. while (e2 > 0 && (body.charCodeAt(e2 - 1) == 32 || body.charCodeAt(e2 - 1) == 9)) { e2 = e2 - 1 }
  507. trail = e2 == 0 ? body : body.slice(e2)
  508. body = body.slice(0, e2)
  509. }
  510. return { level = k text = body trail = trail }
  511. }
  512. // a list item line → { indent, ordered, num, marker, text, col (where its text starts) }
  513. static itemOf = (line) => {
  514. let ind = indentOf(line)
  515. if (ind > 3) { return null }
  516. let t = trimStartSp(line)
  517. let mlen = 0
  518. let ordered = false
  519. let num = 1
  520. let marker = ''
  521. let c0 = t.length > 0 ? t.charCodeAt(0) : 0
  522. if (c0 == 45 || c0 == 42 || c0 == 43) {
  523. mlen = 1
  524. marker = t[0]
  525. } else {
  526. let k = 0
  527. while (k < t.length && k < 9 && t.charCodeAt(k) >= 48 && t.charCodeAt(k) <= 57) { k = k + 1 }
  528. if (k == 0 || k >= t.length) { return null }
  529. let d = t.charCodeAt(k)
  530. if (d != 46 && d != 41) { return null }
  531. mlen = k + 1
  532. ordered = true
  533. num = toNumber(t.slice(0, k))
  534. marker = t[k]
  535. }
  536. if (mlen < t.length && t.charCodeAt(mlen) != 32 && t.charCodeAt(mlen) != 9) { return null }
  537. let rest = t.slice(mlen)
  538. let text = trimStartSp(rest)
  539. let pad = rest.length - text.length
  540. if (pad == 0 || pad > 4) { pad = 1 }
  541. return { indent = ind ordered = ordered num = num marker = marker text = text col = ind + mlen + pad }
  542. }
  543. // a code fence line (``` or ~~~, ≥ 3) → { ch, len, info, indent } or null
  544. static fenceOf = (line) => {
  545. let ind = indentOf(line)
  546. if (ind > 3) { return null }
  547. let t = trimStartSp(line)
  548. if (t.length < 3) { return null }
  549. let c = t.charCodeAt(0)
  550. if (c != 96 && c != 126) { return null }
  551. let k = 0
  552. while (k < t.length && t.charCodeAt(k) == c) { k = k + 1 }
  553. if (k < 3) { return null }
  554. // an info string after ``` must not contain a backtick
  555. if (c == 96 && t.slice(k).includes('`')) { return null }
  556. return { ch = c len = k run = t.slice(0, k) info = t.slice(k).trim() indent = ind }
  557. }
  558. static closesFence = (line, fc) => {
  559. if (indentOf(line) > 3) { return false }
  560. let t = line.trim()
  561. let k = 0
  562. while (k < t.length && t.charCodeAt(k) == fc.ch) { k = k + 1 }
  563. return k >= fc.len && k == t.length
  564. }
  565. // a line of 3+ of one of - * _ (spaces between allowed) and nothing else
  566. static isHrLine = (line) => {
  567. if (indentOf(line) > 3) { return false }
  568. let t = line.trim()
  569. if (t.length < 3) { return false }
  570. let c = t.charCodeAt(0)
  571. if (c != 45 && c != 42 && c != 95) { return false }
  572. let n = 0
  573. let i = 0
  574. while (i < t.length) {
  575. let d = t.charCodeAt(i)
  576. if (d == c) { n = n + 1 } else if (d != 32 && d != 9) { return false }
  577. i = i + 1
  578. }
  579. return n >= 3
  580. }
  581. // `> text` → the text after the marker and one space, or null
  582. static quoteOf = (line) => {
  583. if (indentOf(line) > 3) { return null }
  584. let t = trimStartSp(line)
  585. if (!t.startsWith('>')) { return null }
  586. let r = t.slice(1)
  587. if (r.startsWith(' ')) { r = r.slice(1) }
  588. return r
  589. }
  590. // a table row's cell texts: `| a | b |` → ['a', 'b']; `\|` and code spans do not split
  591. static cellTexts = (line) => {
  592. let t = line.trim()
  593. if (t.startsWith('|')) { t = t.slice(1) }
  594. if (t.endsWith('|') && !t.endsWith(BS + '|')) { t = t.slice(0, t.length - 1) }
  595. let out = []
  596. let cur = ''
  597. let i = 0
  598. while (i < t.length) {
  599. let c = t.charCodeAt(i)
  600. if (c == 92 && i + 1 < t.length) {
  601. cur = cur + t.slice(i, i + 2)
  602. i = i + 2
  603. } else if (c == 96) {
  604. let k = i
  605. while (k < t.length && t.charCodeAt(k) == 96) { k = k + 1 }
  606. let close = runAt(t, k, 96, k - i, false)
  607. let end = close < 0 ? k : close + (k - i)
  608. cur = cur + t.slice(i, end)
  609. i = end
  610. } else if (c == 124) {
  611. out[] = cur.trim()
  612. cur = ''
  613. i = i + 1
  614. } else {
  615. cur = cur + t[i]
  616. i = i + 1
  617. }
  618. }
  619. out[] = cur.trim()
  620. return out
  621. }
  622. static cellsOf = (line, count, align) => {
  623. let texts = cellTexts(line)
  624. let out = []
  625. let i = 0
  626. while (i < count) {
  627. out[] = { spans = spansOf(i < texts.length ? texts[i] : '') align = i < align.length ? align[i] : '' }
  628. i = i + 1
  629. }
  630. return out
  631. }
  632. // is this the `|---|:--:|` line under a table head?
  633. static isSepLine = (line) => {
  634. let t = line.trim()
  635. if (!t.includes('-')) { return false }
  636. if (!t.includes('|') && !t.includes(':')) { return false }
  637. let i = 0
  638. while (i < t.length) {
  639. let d = t.charCodeAt(i)
  640. if (d != 124 && d != 45 && d != 58 && d != 32 && d != 9) { return false }
  641. i = i + 1
  642. }
  643. return true
  644. }
  645. static alignOf = (sep) => {
  646. let out = []
  647. for (c of cellTexts(sep)) {
  648. let l = c.startsWith(':')
  649. let r = c.endsWith(':')
  650. out[] = l && r ? 'center' : (r ? 'right' : (l ? 'left' : ''))
  651. }
  652. return out
  653. }
  654. static tableAt = (&lines, i) => {
  655. if (i + 1 >= lines.length) { return false }
  656. let head = lines[i]
  657. if (!head.includes('|') || indentOf(head) > 3) { return false }
  658. if (!isSepLine(lines[i + 1])) { return false }
  659. return cellTexts(head).length == cellTexts(lines[i + 1]).length
  660. }
  661. // may this line start a block in the middle of a paragraph?
  662. static interrupts = (&lines, j) => {
  663. let line = lines[j]
  664. if (fenceOf(line) != null || headingOf(line) != null || isHrLine(line) || quoteOf(line) != null) { return true }
  665. let it = itemOf(line)
  666. if (it != null && it.text != '' && (!it.ordered || it.num == 1)) { return true }
  667. return tableAt(&lines, j)
  668. }
  669. static block = (kind, gap, raw) => {
  670. let b = { type = kind gap = gap }
  671. // the View's conditions: it takes a flag, never an expression
  672. b.isP = kind == 'p'
  673. b.isHeading = kind == 'heading'
  674. b.isCode = kind == 'code'
  675. b.isList = kind == 'list'
  676. b.isQuote = kind == 'quote'
  677. b.isTable = kind == 'table'
  678. b.isHr = kind == 'hr'
  679. b.isBare = false
  680. // blank lines that held spaces are kept as written
  681. let spaced = false
  682. for (r of raw) { if (r != '') { spaced = true } }
  683. if (spaced) { b.blanks = raw }
  684. return b
  685. }
  686. // the item lines of one list, from lines[i] (an item) on → { list, next }
  687. static listAt = (&lines, i, gap, raw) => {
  688. let n = lines.length
  689. let first = itemOf(lines[i])
  690. let lst = block('list', gap, raw)
  691. lst.ordered = first.ordered
  692. lst.isOl = first.ordered
  693. lst.isUl = !first.ordered
  694. lst.start = first.num
  695. lst.marker = first.marker
  696. lst.indent = first.indent
  697. lst.tight = true
  698. let items = []
  699. let j = i
  700. let itemGap = 0
  701. let going = true
  702. let endAt = n
  703. while (going) {
  704. let it = itemOf(lines[j])
  705. let content = [it.text]
  706. // what each content line lost to the item — written back as it was
  707. let pads = [lines[j].slice(0, lines[j].length - it.text.length)]
  708. let base = it.indent
  709. let cont = -1
  710. let k = j + 1
  711. let blanks = 0
  712. let inItem = true
  713. while (inItem && k < n) {
  714. let l = lines[k]
  715. if (isBlank(l)) {
  716. blanks = blanks + 1
  717. k = k + 1
  718. } else {
  719. let ind = indentOf(l)
  720. if (ind >= base + 2) {
  721. if (cont < 0) { cont = ind < it.col ? ind : it.col }
  722. let b = 0
  723. while (b < blanks) {
  724. content[] = ''
  725. pads[] = lines[k - blanks + b]
  726. b = b + 1
  727. }
  728. blanks = 0
  729. let body = stripCols(l, cont)
  730. content[] = body
  731. pads[] = l.slice(0, l.length - body.length)
  732. k = k + 1
  733. } else if (blanks == 0 && !isBlank(content[content.length - 1]) && itemOf(l) == null && !interrupts(&lines, k)) {
  734. // a lazy continuation of the item's paragraph
  735. let lz = trimStartSp(l)
  736. content[] = lz
  737. pads[] = l.slice(0, l.length - lz.length)
  738. k = k + 1
  739. } else {
  740. inItem = false
  741. }
  742. }
  743. }
  744. let task = null
  745. let t0 = content[0]
  746. if (t0.startsWith('[ ] ') || t0 == '[ ]') {
  747. task = false
  748. } else if (t0.startsWith('[x] ') || t0 == '[x]') {
  749. task = true
  750. }
  751. if (task != null) {
  752. pads[0] = pads[0] + t0.slice(0, 4)
  753. content[0] = t0.slice(4)
  754. }
  755. let sub = parseLines(content)
  756. if (items.length > 0 && itemGap > 0) { lst.tight = false }
  757. let bi = 0
  758. for (sb of sub) {
  759. if (bi > 0 && sb.gap > 0) { lst.tight = false }
  760. bi = bi + 1
  761. }
  762. items[] = { gap = itemGap num = it.num task = task cont = cont < 0 ? it.col : cont pads = pads blocks = sub isTask = task != null done = task == true }
  763. going = false
  764. endAt = k - blanks
  765. if (k < n) {
  766. let nx = itemOf(lines[k])
  767. if (nx != null && nx.ordered == first.ordered && nx.marker == first.marker && nx.indent < base + 2 && !isHrLine(lines[k])) {
  768. itemGap = blanks
  769. j = k
  770. going = true
  771. }
  772. }
  773. }
  774. // a TIGHT list's paragraphs are its items' lines: the View writes them without a <p>
  775. if (lst.tight) {
  776. for (it2 of items) {
  777. for (b2 of it2.blocks) {
  778. if (b2.type == 'p') {
  779. b2.isP = false
  780. b2.isBare = true
  781. }
  782. }
  783. }
  784. }
  785. lst.items = items
  786. return { list = lst next = endAt }
  787. }
  788. // lines → blocks
  789. static parseLines = (&lines) => {
  790. let out = []
  791. let gap = 0
  792. let blanks = []
  793. let i = 0
  794. let n = lines.length
  795. while (i < n) {
  796. let line = lines[i]
  797. let fence = fenceOf(line)
  798. if (isBlank(line)) {
  799. gap = gap + 1
  800. blanks[] = line
  801. i = i + 1
  802. } else if (indentOf(line) >= 4) {
  803. // an indented code block: indented lines, and blank lines between them
  804. let j = i
  805. let more = true
  806. while (j < n && more) {
  807. if (isBlank(lines[j])) {
  808. let k = j
  809. while (k < n && isBlank(lines[k])) { k = k + 1 }
  810. if (k < n && indentOf(lines[k]) >= 4) { j = k } else { more = false }
  811. } else if (indentOf(lines[j]) >= 4) {
  812. j = j + 1
  813. } else {
  814. more = false
  815. }
  816. }
  817. let code = ''
  818. let k2 = i
  819. while (k2 < j) {
  820. code = code + (isBlank(lines[k2]) ? '' : stripCols(lines[k2], 4)) + NL
  821. k2 = k2 + 1
  822. }
  823. let cb = block('code', gap, blanks)
  824. cb.code = code
  825. cb.lang = ''
  826. cb.langClass = ''
  827. cb.info = ''
  828. cb.fence = ''
  829. cb.closed = true
  830. out[] = cb
  831. gap = 0
  832. blanks = []
  833. i = j
  834. } else if (fence != null) {
  835. let j = i + 1
  836. let closed = false
  837. let code = ''
  838. let closeSrc = ''
  839. let cpads = []
  840. while (j < n && !closed) {
  841. if (closesFence(lines[j], fence)) {
  842. closed = true
  843. closeSrc = lines[j]
  844. } else {
  845. let cl = stripCols(lines[j], fence.indent)
  846. code = code + cl + NL
  847. cpads[] = lines[j].slice(0, lines[j].length - cl.length)
  848. }
  849. j = j + 1
  850. }
  851. let fb = block('code', gap, blanks)
  852. fb.code = code
  853. fb.info = fence.info
  854. let sp = fence.info.indexOf(' ')
  855. fb.lang = sp < 0 ? fence.info : fence.info.slice(0, sp)
  856. fb.langClass = fb.lang == '' ? '' : 'language-' + fb.lang
  857. fb.fence = fence.run
  858. fb.closed = closed
  859. fb.open = line
  860. fb.close = closeSrc
  861. if (fence.indent > 0) { fb.pads = cpads }
  862. out[] = fb
  863. gap = 0
  864. blanks = []
  865. i = j
  866. } else if (isHrLine(line)) {
  867. let hb = block('hr', gap, blanks)
  868. hb.src = line
  869. out[] = hb
  870. gap = 0
  871. blanks = []
  872. i = i + 1
  873. } else if (headingOf(line) != null) {
  874. let h = headingOf(line)
  875. let hd = block('heading', gap, blanks)
  876. hd.level = h.level
  877. hd.isH1 = h.level == 1
  878. hd.isH2 = h.level == 2
  879. hd.isH3 = h.level == 3
  880. hd.isH4 = h.level == 4
  881. hd.isH5 = h.level == 5
  882. hd.isH6 = h.level == 6
  883. hd.spans = spansOf(h.text)
  884. hd.trail = h.trail
  885. hd.src = line
  886. out[] = hd
  887. gap = 0
  888. blanks = []
  889. i = i + 1
  890. } else if (quoteOf(line) != null) {
  891. let ql = []
  892. let qp = []
  893. let j = i
  894. while (j < n && quoteOf(lines[j]) != null) {
  895. let qt = quoteOf(lines[j])
  896. ql[] = qt
  897. qp[] = lines[j].slice(0, lines[j].length - qt.length)
  898. j = j + 1
  899. }
  900. let qb = block('quote', gap, blanks)
  901. // blank lines at the quote's end belong to it (`>` alone), not to what follows
  902. let qtail = 0
  903. while (ql.length > 0 && isBlank(ql[ql.length - 1])) {
  904. ql.pop()
  905. qtail = qtail + 1
  906. }
  907. qb.blocks = parseLines(ql)
  908. qb.tail = qtail
  909. qb.pads = qp
  910. out[] = qb
  911. gap = 0
  912. blanks = []
  913. i = j
  914. } else if (tableAt(&lines, i)) {
  915. let count = cellTexts(line).length
  916. let tb = block('table', gap, blanks)
  917. tb.align = alignOf(lines[i + 1])
  918. tb.head = { cells = cellsOf(line, count, tb.align) src = line }
  919. tb.sep = lines[i + 1]
  920. tb.rows = []
  921. let j = i + 2
  922. while (j < n && !isBlank(lines[j]) && lines[j].includes('|')) {
  923. tb.rows[] = { cells = cellsOf(lines[j], count, tb.align) src = lines[j] }
  924. j = j + 1
  925. }
  926. out[] = tb
  927. gap = 0
  928. blanks = []
  929. i = j
  930. } else if (itemOf(line) != null) {
  931. let r = listAt(&lines, i, gap, blanks)
  932. out[] = r.list
  933. gap = 0
  934. blanks = []
  935. i = r.next
  936. } else {
  937. let para = [line]
  938. let j = i + 1
  939. while (j < n && !isBlank(lines[j]) && !interrupts(&lines, j)) {
  940. para[] = lines[j]
  941. j = j + 1
  942. }
  943. let pb = block('p', gap, blanks)
  944. pb.spans = spansOf(para.join(NL))
  945. out[] = pb
  946. gap = 0
  947. blanks = []
  948. i = j
  949. }
  950. }
  951. return out
  952. }
  953. // the Markdown text → the document (see the header)
  954. static parse = (text) => {
  955. let lines = []
  956. // a CR before a newline (Windows line ends) is dropped; a document whose every
  957. // line ends in CR LF is written back that way (`eol`)
  958. let crs = 0
  959. for (l of (text == null ? '' : '' + text).split(NL)) {
  960. if (l.length > 0 && l.charCodeAt(l.length - 1) == 13) {
  961. crs = crs + 1
  962. lines[] = l.slice(0, l.length - 1)
  963. } else {
  964. lines[] = l
  965. }
  966. }
  967. let crlf = crs > 0 && crs == lines.length - 1
  968. let tail = 0
  969. let end = ''
  970. while (lines.length > 1 && isBlank(lines[lines.length - 1])) {
  971. end = NL + lines.pop() + end
  972. tail = tail + 1
  973. }
  974. let doc = { blocks = [] tail = tail }
  975. if (crlf) {
  976. // the CR LF pair, taken from the text itself (the language writes no CR)
  977. let at = ('' + text).indexOf(NL)
  978. doc.eol = ('' + text).slice(at - 1, at + 1)
  979. }
  980. // trailing blank lines that held spaces are kept as written
  981. if (end != rep(NL, tail)) { doc.end = end }
  982. if (lines.length == 1 && isBlank(lines[0])) { return doc }
  983. doc.blocks = parseLines(&lines)
  984. return doc
  985. }
  986. // ---- writing ------------------------------------------------------------------------------
  987. static flagOf = (v) => { return v == true }
  988. static strOf = (v) => { return v == null ? '' : '' + v }
  989. // what a span MEANS — everything but its source
  990. static spanKey = (s) => {
  991. return [strOf(s.text), flagOf(s.strong), flagOf(s.em), flagOf(s.strike), flagOf(s.code), strOf(s.href), flagOf(s.image), flagOf(s.br)]
  992. }
  993. static sameSpans = (a, b) => {
  994. let ka = []
  995. for (x of a) {
  996. // an empty text span carries source only
  997. if (!(strOf(x.text) == '' && !flagOf(x.code) && !flagOf(x.image))) { ka[] = spanKey(x) }
  998. }
  999. let kb = []
  1000. for (y of b) {
  1001. if (!(strOf(y.text) == '' && !flagOf(y.code) && !flagOf(y.image))) { kb[] = spanKey(y) }
  1002. }
  1003. return JSON.stringify(ka) == JSON.stringify(kb)
  1004. }
  1005. // text as Markdown that reads back as the same text
  1006. static escapeText = (t) => {
  1007. let out = ''
  1008. let i = 0
  1009. while (i < t.length) {
  1010. let c = t.charCodeAt(i)
  1011. let prev = i > 0 ? t.charCodeAt(i - 1) : 32
  1012. let next = i + 1 < t.length ? t.charCodeAt(i + 1) : 32
  1013. if (c == 92 || c == 42 || c == 96 || c == 91 || c == 93 || c == 60 || c == 124 || (c == 126 && (prev == 126 || next == 126))) {
  1014. out = out + BS + t[i]
  1015. } else if (c == 95 && (!isAlnumC(prev) || !isAlnumC(next))) {
  1016. out = out + BS + t[i]
  1017. } else {
  1018. out = out + t[i]
  1019. }
  1020. i = i + 1
  1021. }
  1022. return out
  1023. }
  1024. static codeMd = (t) => {
  1025. let run = 1
  1026. while (t.includes(rep('`', run))) { run = run + 1 }
  1027. let pad = t.startsWith('`') || t.endsWith('`') ? ' ' : ''
  1028. return rep('`', run) + pad + t + pad + rep('`', run)
  1029. }
  1030. static targetMd = (h) => { return h.replaceAll(')', '%29').replaceAll(' ', '%20') }
  1031. // spans → canonical inline Markdown: a link outermost, then ~~, **, *
  1032. static canonicalInline = (spans) => {
  1033. let out = ''
  1034. let cur = { href = '' strike = false strong = false em = false }
  1035. for (s of spans) {
  1036. let want = { href = s.image ? '' : strOf(s.href) strike = flagOf(s.strike) strong = flagOf(s.strong) em = flagOf(s.em) }
  1037. if (flagOf(s.br)) { want = { href = cur.href strike = cur.strike strong = cur.strong em = cur.em } }
  1038. // the outermost level that changes; everything inside it closes and reopens
  1039. let lvl = 4
  1040. if (want.em != cur.em) { lvl = 3 }
  1041. if (want.strong != cur.strong) { lvl = 2 }
  1042. if (want.strike != cur.strike) { lvl = 1 }
  1043. if (want.href != cur.href) { lvl = 0 }
  1044. if (lvl < 4) {
  1045. if (cur.em && lvl <= 3) { out = out + '*' }
  1046. if (cur.strong && lvl <= 2) { out = out + '**' }
  1047. if (cur.strike && lvl <= 1) { out = out + '~~' }
  1048. if (cur.href != '' && lvl == 0) { out = out + '](' + targetMd(cur.href) + ')' }
  1049. if (want.href != '' && lvl == 0) { out = out + '[' }
  1050. if (want.strike && lvl <= 1) { out = out + '~~' }
  1051. if (want.strong && lvl <= 2) { out = out + '**' }
  1052. if (want.em && lvl <= 3) { out = out + '*' }
  1053. cur = want
  1054. }
  1055. if (flagOf(s.br)) {
  1056. out = out + NL
  1057. } else if (flagOf(s.code)) {
  1058. out = out + codeMd(strOf(s.text))
  1059. } else if (flagOf(s.image)) {
  1060. out = out + '![' + escapeText(strOf(s.text)) + '](' + targetMd(strOf(s.href)) + ')'
  1061. } else {
  1062. out = out + escapeText(strOf(s.text))
  1063. }
  1064. }
  1065. if (cur.em) { out = out + '*' }
  1066. if (cur.strong) { out = out + '**' }
  1067. if (cur.strike) { out = out + '~~' }
  1068. if (cur.href != '') { out = out + '](' + targetMd(cur.href) + ')' }
  1069. return out
  1070. }
  1071. // spans → inline Markdown: the source they came from when it still reads as them
  1072. static inlineMd = (spans) => {
  1073. if (spans == null) { return '' }
  1074. let cand = ''
  1075. let all = true
  1076. for (s of spans) {
  1077. if (s.src == null) { all = false } else { cand = cand + s.src }
  1078. }
  1079. if (all && sameSpans(spansOf(cand), spans)) { return cand }
  1080. return canonicalInline(spans)
  1081. }
  1082. // a written paragraph line that would start a block gets its first character escaped
  1083. static guardLine = (l) => {
  1084. let t = trimStartSp(l)
  1085. if (t == '') { return l }
  1086. let lead = l.slice(0, l.length - t.length)
  1087. if (headingOf(t) != null || fenceOf(t) != null || isHrLine(t) || quoteOf(t) != null || t.startsWith('+ ') || t.startsWith('- ') || t.startsWith('= ')) {
  1088. return lead + BS + t
  1089. }
  1090. let it = itemOf(t)
  1091. if (it != null && it.ordered) {
  1092. let k = 0
  1093. while (k < t.length && t.charCodeAt(k) >= 48 && t.charCodeAt(k) <= 57) { k = k + 1 }
  1094. return lead + t.slice(0, k) + BS + t.slice(k)
  1095. }
  1096. return l
  1097. }
  1098. static tableRowMd = (row) => {
  1099. let cells = []
  1100. for (c of row.cells) { cells[] = inlineMd(c.spans) }
  1101. if (row.src != null) {
  1102. // the row as written, when its cells (missing ones empty, extra ones dropped) still read the same
  1103. let texts = cellTexts(row.src)
  1104. let same = true
  1105. let i = 0
  1106. while (i < cells.length) {
  1107. if ((i < texts.length ? texts[i] : '') != cells[i]) { same = false }
  1108. i = i + 1
  1109. }
  1110. if (same) { return row.src }
  1111. }
  1112. return '| ' + cells.join(' | ') + ' |'
  1113. }
  1114. // one block → its lines
  1115. static blockLines = (b) => {
  1116. let out = []
  1117. let kind = strOf(b.type)
  1118. if (kind == 'p') {
  1119. let md = inlineMd(b.spans)
  1120. let src = true
  1121. for (s of b.spans) { if (s.src == null) { src = false } }
  1122. for (l of md.split(NL)) { out[] = src && md == inlineMdSrc(b.spans) ? l : guardLine(l) }
  1123. } else if (kind == 'heading') {
  1124. let lv = b.level == null ? 1 : b.level
  1125. if (lv < 1) { lv = 1 }
  1126. if (lv > 6) { lv = 6 }
  1127. let md2 = inlineMd(b.spans).replaceAll(NL, ' ')
  1128. let h = b.src == null ? null : headingOf(b.src)
  1129. if (h != null && h.level == lv && h.trail == strOf(b.trail) && h.text == md2) {
  1130. out[] = b.src
  1131. } else {
  1132. out[] = rep('#', lv) + (md2 == '' ? '' : ' ' + md2) + strOf(b.trail)
  1133. }
  1134. } else if (kind == 'code') {
  1135. let code = strOf(b.code)
  1136. let ls = code.split(NL)
  1137. if (code.endsWith(NL)) { ls.pop() }
  1138. if (code == '') { ls = [] }
  1139. if (strOf(b.fence) == '' && b.fence != null && code.trim() != '') {
  1140. for (l of ls) { out[] = l == '' ? '' : ' ' + l }
  1141. } else {
  1142. let fence = strOf(b.fence)
  1143. if (fence == '') { fence = '```' }
  1144. let fc = fence.charCodeAt(0)
  1145. // a fence longer than any fence-like line inside the code
  1146. for (l of ls) {
  1147. let t = l.trim()
  1148. let k = 0
  1149. while (k < t.length && t.charCodeAt(k) == fc) { k = k + 1 }
  1150. if (k >= fence.length && k == t.length) { fence = rep(fence[0], k + 1) }
  1151. }
  1152. let info = strOf(b.info)
  1153. if (info == '') { info = strOf(b.lang) }
  1154. let fo = b.open == null ? null : fenceOf(b.open)
  1155. let asWritten = fo != null && fo.run == fence && fo.info == info
  1156. out[] = asWritten ? b.open : fence + info
  1157. let cp = asWritten && b.pads != null && b.pads.length == ls.length ? b.pads : null
  1158. let ci = 0
  1159. for (l of ls) {
  1160. out[] = cp != null ? cp[ci] + l : l
  1161. ci = ci + 1
  1162. }
  1163. if (b.closed != false) { out[] = asWritten && b.close != null && closesFence(b.close, fo) ? b.close : fence }
  1164. }
  1165. } else if (kind == 'hr') {
  1166. out[] = isHrLine(strOf(b.src)) ? strOf(b.src) : '---'
  1167. } else if (kind == 'quote') {
  1168. let ql = linesOf(b.blocks == null ? [] : b.blocks, false)
  1169. let qt = 0
  1170. while (qt < (b.tail == null ? 0 : b.tail)) {
  1171. ql[] = ''
  1172. qt = qt + 1
  1173. }
  1174. let qp = b.pads != null && b.pads.length == ql.length ? b.pads : null
  1175. let qi = 0
  1176. for (l of ql) {
  1177. if (qp != null && quoteOf(qp[qi] + l) != null) {
  1178. out[] = qp[qi] + l
  1179. } else {
  1180. out[] = l == '' ? '>' : '> ' + l
  1181. }
  1182. qi = qi + 1
  1183. }
  1184. } else if (kind == 'table') {
  1185. let head = b.head == null ? { cells = [] } : b.head
  1186. let cols = head.cells.length
  1187. out[] = tableRowMd(head)
  1188. let sep = strOf(b.sep)
  1189. let al = b.align == null ? [] : b.align
  1190. if (!isSepLine(sep) || JSON.stringify(alignOf(sep)) != JSON.stringify(al) || cellTexts(sep).length != cols) {
  1191. let parts = []
  1192. let i = 0
  1193. while (i < cols) {
  1194. let a = i < al.length ? al[i] : ''
  1195. parts[] = a == 'center' ? ':---:' : (a == 'right' ? '---:' : (a == 'left' ? ':---' : '---'))
  1196. i = i + 1
  1197. }
  1198. sep = '| ' + parts.join(' | ') + ' |'
  1199. }
  1200. out[] = sep
  1201. for (r of (b.rows == null ? [] : b.rows)) { out[] = tableRowMd(r) }
  1202. } else if (kind == 'list') {
  1203. let ordered = b.ordered == true
  1204. let marker = strOf(b.marker)
  1205. if (ordered && marker != '.' && marker != ')') { marker = '.' }
  1206. if (!ordered && marker != '-' && marker != '*' && marker != '+') { marker = '-' }
  1207. let lead = rep(' ', b.indent == null ? 0 : b.indent)
  1208. let start = b.start == null ? 1 : b.start
  1209. let idx = 0
  1210. for (it of b.items) {
  1211. let g = 0
  1212. while (g < (it.gap == null ? 0 : it.gap)) {
  1213. out[] = ''
  1214. g = g + 1
  1215. }
  1216. let num = it.num == null ? start + idx : it.num
  1217. let mark = lead + (ordered ? '' + num + marker : marker)
  1218. let task = it.task == true ? '[x] ' : (it.task == false ? '[ ] ' : '')
  1219. let cont = it.cont == null ? mark.length + 1 : it.cont
  1220. let pad = rep(' ', cont)
  1221. let ls = linesOf(it.blocks == null ? [] : it.blocks, true)
  1222. // the item's lines as they were written, while they still fit
  1223. let ip = it.pads != null && it.pads.length == (ls.length == 0 ? 1 : ls.length) && it.pads[0].trim() == (mark + ' ' + task).trim() ? it.pads : null
  1224. if (ls.length == 0) {
  1225. out[] = ip != null ? ip[0] : mark + (task == '' ? '' : ' ' + trimEndSp(task))
  1226. } else {
  1227. let fl = 0
  1228. for (l of ls) {
  1229. if (fl == 0) {
  1230. out[] = ip != null ? ip[0] + l : (l == '' && task == '' ? mark : mark + ' ' + task + l)
  1231. } else if (ip != null) {
  1232. out[] = ip[fl] + l
  1233. } else {
  1234. out[] = l == '' ? '' : pad + l
  1235. }
  1236. fl = fl + 1
  1237. }
  1238. }
  1239. idx = idx + 1
  1240. }
  1241. }
  1242. return out
  1243. }
  1244. static inlineMdSrc = (spans) => {
  1245. let cand = ''
  1246. for (s of spans) { cand = cand + strOf(s.src) }
  1247. return cand
  1248. }
  1249. // blocks → lines, with their blank lines between them. A block with no `gap` (built
  1250. // by hand) gets one blank line before it — none inside a list item (`inItem`), except
  1251. // between two paragraphs, which would otherwise read back as one
  1252. static linesOf = (blocks, inItem) => {
  1253. let out = []
  1254. let bi = 0
  1255. let prevKind = ''
  1256. for (b of blocks) {
  1257. let g = 0
  1258. let want = b.gap
  1259. if (want == null) {
  1260. want = bi == 0 ? 0 : 1
  1261. if (inItem && !(prevKind == 'p' && b.type == 'p')) { want = 0 }
  1262. }
  1263. prevKind = strOf(b.type)
  1264. let raw = b.blanks != null && b.blanks.length == want ? b.blanks : null
  1265. while (g < want) {
  1266. out[] = raw != null ? raw[g] : ''
  1267. g = g + 1
  1268. }
  1269. for (l of blockLines(b)) { out[] = l }
  1270. bi = bi + 1
  1271. }
  1272. return out
  1273. }
  1274. // the document (or a bare array of blocks) → Markdown text
  1275. static serialize = (doc) => {
  1276. if (doc == null) { return '' }
  1277. let blocks = doc
  1278. let tail = 0
  1279. if (doc.blocks != null) {
  1280. blocks = doc.blocks
  1281. tail = doc.tail == null ? 0 : doc.tail
  1282. }
  1283. let end = rep(NL, tail)
  1284. if (doc.end != null && doc.end.split(NL).length == tail + 1 && doc.end.trim() == '') { end = doc.end }
  1285. let out = linesOf(blocks, false).join(NL) + end
  1286. // a CR LF document: every line end gets its CR back
  1287. if (doc.eol != null && doc.eol.length == 2 && doc.eol.charCodeAt(0) == 13) { out = out.split(NL).join(doc.eol) }
  1288. return out
  1289. }

Branches

Latest commits

  • 69b0a0ceident#34: access: the pick buttons use Style.pickCard instead of class pickmre
  • dc702419Merge branch 'f9-choose' into mainIdent
  • 8caec771|||PR ident#34 restore .choose hookmre
  • 0dbff960Merge branch 'f8-shared' into mainIdent
  • d69665f6|||PR ident#34 part 3: shared look classes (wide, avatar, chip/used, conn, choose, who, inboxlink) moved to statics or droppedmre
  • 3a64dc48ident#34: chipLook: radius as the same var() string (a bare token name in a static does not compile); selector gate selectormre
  • eb039073ident#34: tests: signin and migration find the choice without the classmre
  • 4b94a594ident#34: tests: fix selector of the identity choicemre
  • bebc88e4ident#34: main.hl drops the unstyled classes inboxlink and whomre
  • b4e7ad04ident#34: the identity choice link/button has no class (rule on #chooselist a)mre
  • 3c33d6eaident#34: the connection link in inbox has no class (rule on #conns a)mre
  • dc97ec0dident#34: the Notif. chip and the Used in link share chipLook (Style.pChip) instead of the classes chip/used/notifmre
  • 378bd432ident#34: the identity picture look is a static in styles.hl (Style.pAvatar); sizes stay with each listmre
  • 1cf79e7eident#34: the wide page column is a static in styles.hl (Style.pWide), not the class "wide"mre
  • ddc7d135Merge branch 'f7-hooks' into mainIdent
  • eee0b513|||PR ident#34 part: test-hook classes replaced by name/element/attribute selectors (tests adjusted), the shared lead/message/notice looks are statics in styles.hlmre
  • ccd6d0c4ident#34: browser gate: rowButton finds the row's button by namemre
  • 24dd414eident#34: the shared lead, message and notice looks are statics in components/styles.hl, imported by the components (lib/looks.hl gone)mre
  • 03ed26c7ident#34: access.hl: the remaining lead classes droppedmre
  • 1d066c42ident#34: access.hl drops its unstyled lead class (the shared lead rule would otherwise mute it)mre