tickets
All repositories: gitoria
20.0 KB
// markdown.hl — MARKDOWN IN TEXT FIELDS (ticket tickets.worldapi.org#6, mission 014; CONCEPT.md// "Data formats": Markdown inside text fields). The summary, comment texts and state notes are// Markdown; the ticket page renders them. Statics only, server realm.//// SAFE BY CONSTRUCTION: the text is parsed HERE into plain data — blocks of spans — and the// View (components/markdown.hl) builds elements from it. Every character of the source ends// up in a TEXT node (hl:web escapes it); there is no HTML string anywhere, so raw HTML in the// source (`<script>`, `<img onerror>`) is shown as the text it is. A link's href is taken only// if `safeHref` accepts it: http(s):, mailto:, a same-site path (`/…`, not `//…`) or `#…`;// anything else (`javascript:`, `data:`, `vbscript:`, relative `foo`, quotes, spaces, control// characters) stays plain text.//// WHAT IS UNDERSTOOD (the subset ticket #6 names — headings, lists, code, links, emphasis):// blocks: `#`…`######` headings, `-`/`*`/`+` and `1.`/`1)` list items (one level; deeper// indentation is flattened; a list may interrupt a paragraph — the import writes// them that way), ``` / ~~~ fenced code, paragraphs (blank-line separated; their// single line breaks are KEPT — the page showed them before, `white-space: pre-wrap`)// inline: `code` (any backtick run), [text](url), <https://…>, bare http(s):// URLs,// *em* _em_ **strong** __strong__ ***both***, backslash escapes of punctuation// tables: GitHub pipe tables (mission 013, the rules of notes#5 — see "tables" below)// Not understood (shown as typed): block quotes, images, nested lists, HTML.//// A block: { isP | isH1 | isH2 | isH3 | isUl | isOl | isCode, spans, items: [{ spans }],// start (ol), code (text of a code block) }; a table block also has isTable = true,// head: [{ spans, align }] and rows: [{ cells: [{ spans, align }] }] (align '' | left |// center | right). The other blocks carry no isTable (their page data stays as it was).// A span: { text, isText | isStrong | isEm | isStrongEm | isCode | isLink, href }static NL = ""static isSpaceC = (c) => { return c == 32 || c == 9 || c == 10 || c == 13 }static isAlnumC = (c) => { return (c >= 48 && c <= 57) || (c >= 65 && c <= 90) || (c >= 97 && c <= 122) || c > 127 }static isPunctC = (c) => { return (c >= 33 && c <= 47) || (c >= 58 && c <= 64) || (c >= 91 && c <= 96) || (c >= 123 && c <= 126) }// ---- links ------------------------------------------------------------------------------// the href of a link, or null when it is not one we let throughstatic safeHref = (u) => {if (u == null) { return null }t = ('' + u).trim()if (t == '' || t.length > 2000) { return null }let i = 0while (i < t.length) {let c = t.charCodeAt(i)// no spaces / control characters, no quotes, no < > ` and no backslashif (c <= 32 || c == 127 || c == 34 || c == 39 || c == 60 || c == 62 || c == 96 || c == 92) { return null }i = i + 1}low = t.toLowerCase()if (low.startsWith('https://') && t.length > 8) { return t }if (low.startsWith('http://') && t.length > 7) { return t }if (low.startsWith('mailto:') && t.length > 7) { return t }if (t.startsWith('/') && !t.startsWith('//')) { return t }if (t.startsWith('#')) { return t }return null}static isUrlStart = (s, i) => {low = s.slice(i, i + 8).toLowerCase()return low.startsWith('https://') || low.startsWith('http://')}// where a bare URL starting at i ends: up to whitespace or < > " `, minus trailing punctuation// (and a closing parenthesis that has no opening one inside the URL)static isUrlStop = (c) => { return isSpaceC(c) || c == 60 || c == 62 || c == 34 || c == 96 }static urlEnd = (s, i) => {let end = iwhile (end < s.length && !isUrlStop(s.charCodeAt(end))) { end = end + 1 }let trimming = truewhile (trimming && end > i) {let c = s.charCodeAt(end - 1)if (c == 46 || c == 44 || c == 59 || c == 58 || c == 33 || c == 63 || c == 39 || c == 42 || c == 95) {end = end - 1} else if (c == 41) {let opens = 0let closes = 0let k = iwhile (k < end) {if (s.charCodeAt(k) == 40) { opens = opens + 1 }if (s.charCodeAt(k) == 41) { closes = closes + 1 }k = k + 1}if (closes > opens) { end = end - 1 } else { trimming = false }} else {trimming = false}}return end}// `[label](url)` at i → { label, href (null = not allowed), end } or null (not a link)static linkAt = (s, i) => {let j = i + 1let found = -1while (j < s.length && found < 0) {let c = s.charCodeAt(j)if (c == 91) { return null }if (c == 93) { found = j }j = j + 1}if (found < 0 || found == i + 1) { return null }if (found + 1 >= s.length || s.charCodeAt(found + 1) != 40) { return null }k = s.indexOf(')', found + 2)if (k < 0) { return null }let target = s.slice(found + 2, k).trim()// an optional title after a space is dropped: [x](url "title")sp = target.indexOf(' ')if (sp > 0) { target = target.slice(0, sp) }return { label = s.slice(i + 1, found) href = safeHref(target) end = k + 1 }}// ---- spans --------------------------------------------------------------------------------static textSpan = (t, strong, em) => {return { text = t isText = !strong && !em isStrong = strong && !em isEm = em && !strong isStrongEm = strong && em isCode = false isLink = false href = '' }}static codeSpan = (t) => {return { text = t isText = false isStrong = false isEm = false isStrongEm = false isCode = true isLink = false href = '' }}static linkSpan = (t, h) => {return { text = t isText = false isStrong = false isEm = false isStrongEm = false isCode = false isLink = true href = h }}// the index of a run of EXACTLY n characters c at or after `at`, or -1. `esc`: a character// after a backslash does not count (emphasis; inside `code` a backslash is just a backslash)static runAt = (s, at, c, n, esc) => {let i = atwhile (i < s.length) {if (esc && s.charCodeAt(i) == 92) {i = i + 2} else if (s.charCodeAt(i) == c) {let k = iwhile (k < s.length && s.charCodeAt(k) == c) { k = k + 1 }if (k - i == n) { return i }i = k} else {i = i + 1}}return -1}// is there a CLOSING delimiter run (exactly n of c, not after a space; `_` not before a// letter/digit) after `at`?static hasCloser = (s, at, c, n) => {let i = atwhile (i < s.length) {let r = runAt(s, i, c, n, true)if (r < 0) { return false }let prev = r > 0 ? s.charCodeAt(r - 1) : 32let next = r + n < s.length ? s.charCodeAt(r + n) : 32if (r > at && !isSpaceC(prev) && (c != 95 || !isAlnumC(next))) { return true }i = r + n}return false}static inlineSpans = (src) => {out = []if (src == null || src == '') { return out }s = '' + srcn = s.lengthlet buf = ''let strong = falselet em = falselet strongC = 0let emC = 0let i = 0while (i < n) {let c = s.charCodeAt(i)if (c == 92 && i + 1 < n && isPunctC(s.charCodeAt(i + 1))) {// a backslash escape: the next punctuation character as itselfbuf = buf + s[i + 1]i = i + 2} else if (c == 96) {// `code`: a run of backticks up to the same runlet k = iwhile (k < n && s.charCodeAt(k) == 96) { k = k + 1 }let close = runAt(s, k, 96, k - i, false)if (close < 0) {buf = buf + s.slice(i, k)i = k} else {if (buf != '') { out.push(textSpan(buf, strong, em)) }buf = ''let code = s.slice(k, close).replaceAll(NL, ' ')if (code.length > 2 && code.startsWith(' ') && code.endsWith(' ') && code.trim() != '') { code = code.slice(1, code.length - 1) }out.push(codeSpan(code))i = close + (k - i)}} else if (c == 91) {// [label](url)let l = linkAt(s, i)if (l == null) {buf = buf + '['i = i + 1} else if (l.href == null) {buf = buf + s.slice(i, l.end)i = l.end} else {if (buf != '') { out.push(textSpan(buf, strong, em)) }buf = ''out.push(linkSpan(l.label, l.href))i = l.end}} else if (c == 60 && i + 1 < n && isUrlStart(s, i + 1)) {// <https://…>let e = s.indexOf('>', i + 1)let url = e > 0 ? s.slice(i + 1, e) : ''let h = safeHref(url)if (e < 0 || h == null) {buf = buf + '<'i = i + 1} else {if (buf != '') { out.push(textSpan(buf, strong, em)) }buf = ''out.push(linkSpan(url, h))i = e + 1}} else if ((c == 104 || c == 72) && (i == 0 || !isAlnumC(s.charCodeAt(i - 1))) && isUrlStart(s, i)) {// a bare http(s):// URLlet e = urlEnd(s, i)let url = s.slice(i, e)let h = safeHref(url)if (h == null) {buf = buf + s[i]i = i + 1} else {if (buf != '') { out.push(textSpan(buf, strong, em)) }buf = ''out.push(linkSpan(url, h))i = e}} else if (c == 42 || c == 95) {// * and _: emphasis (1), strong (2), both (3)let k = iwhile (k < n && s.charCodeAt(k) == c) { k = k + 1 }let run = k - ilet prev = i > 0 ? s.charCodeAt(i - 1) : 32let next = k < n ? s.charCodeAt(k) : 32let wantStrong = run == 2 || run == 3let wantEm = run == 1 || run == 3let canOpen = !isSpaceC(next) && (c == 42 || !isAlnumC(prev))let canClose = !isSpaceC(prev) && (c == 42 || !isAlnumC(next))let closes = canClose && run <= 3 && (!wantStrong || (strong && strongC == c)) && (!wantEm || (em && emC == c))let opens = !closes && canOpen && run <= 3 && (!wantStrong || !strong) && (!wantEm || !em) && hasCloser(s, k, c, run)if (closes || opens) {if (buf != '') { out.push(textSpan(buf, strong, em)) }buf = ''if (wantStrong) {strong = opensstrongC = c}if (wantEm) {em = opensemC = c}} else {buf = buf + s.slice(i, k)}i = k} else {buf = buf + s[i]i = i + 1}}if (buf != '') { out.push(textSpan(buf, strong, em)) }return out}// ---- blocks -------------------------------------------------------------------------------static noCr = (line) => { return line.length > 0 && line.charCodeAt(line.length - 1) == 13 ? line.slice(0, line.length - 1) : line }static indentOf = (line) => {let i = 0while (i < line.length && (line.charCodeAt(i) == 32 || line.charCodeAt(i) == 9)) { i = i + 1 }return i}// a heading line (≤ 3 spaces in, 1–6 '#', a space or the end) → { level, text } or nullstatic headingOf = (line) => {if (indentOf(line) > 3) { return null }t = line.trim()let k = 0while (k < t.length && t.charCodeAt(k) == 35) { k = k + 1 }if (k == 0 || k > 6) { return null }if (k < t.length && t.charCodeAt(k) != 32 && t.charCodeAt(k) != 9) { return null }let body = t.slice(k).trim()// a closing run of '#' after a space goeslet e = body.lengthwhile (e > 0 && body.charCodeAt(e - 1) == 35) { e = e - 1 }if (e == 0) { body = '' } else if (e < body.length && (body.charCodeAt(e - 1) == 32 || body.charCodeAt(e - 1) == 9)) { body = body.slice(0, e).trim() }return { level = k text = body }}// a list item line → { ordered, start, text } or nullstatic itemOf = (line) => {t = line.trim()if (t.length >= 2 && (t.charCodeAt(0) == 45 || t.charCodeAt(0) == 42 || t.charCodeAt(0) == 43) && (t.charCodeAt(1) == 32 || t.charCodeAt(1) == 9)) {return { ordered = false start = 1 text = t.slice(2).trim() }}if (t.length == 1 && (t == '-' || t == '*' || t == '+')) { return { ordered = false start = 1 text = '' } }let k = 0while (k < t.length && k < 9 && t.charCodeAt(k) >= 48 && t.charCodeAt(k) <= 57) { k = k + 1 }if (k == 0 || k >= t.length) { return null }d = t.charCodeAt(k)if (d != 46 && d != 41) { return null }if (k + 1 < t.length && t.charCodeAt(k + 1) != 32 && t.charCodeAt(k + 1) != 9) { return null }return { ordered = true start = toNumber(t.slice(0, k)) text = t.slice(k + 1).trim() }}// a code fence line (``` or ~~~, ≥ 3) → its run (e.g. '````') or nullstatic fenceOf = (line) => {if (indentOf(line) > 3) { return null }t = line.trim()if (t.length < 3) { return null }c = t.charCodeAt(0)if (c != 96 && c != 126) { return null }let k = 0while (k < t.length && t.charCodeAt(k) == c) { k = k + 1 }if (k < 3) { return null }// an info string after ``` must not contain a backtickif (c == 96 && t.slice(k).includes('`')) { return null }return t.slice(0, k)}// ---- tables (GitHub pipe tables, mission 013 = notes#5's rules, notes reports/003-md-tables.md) ----// A table is a HEADER row with a DELIMITER row right under it that has the same number of cells// (`|---|:--:|--:|`: each cell `:?-+:?`, the colons set left / center / right; it must hold a `|`).// BODY rows follow up to a blank line, a heading, a fence or a list item; a short row is padded// with empty cells, a long one cut. Cells are split at every `|` that is not escaped (`\|` is a// pipe in the cell, also inside `code`); the outer pipes are optional; a cell is inline Markdown.// A table may interrupt a paragraph. A line with pipes but no delimiter row under it stays text.// is the character at k escaped: an odd run of backslashes right before it (not before `start`)?static escapedAt = (s, k, start) => {let n = 0while (k - 1 - n >= start && s.charCodeAt(k - 1 - n) == 92) { n = n + 1 }return n % 2 == 1}// a row's cells (trimmed source texts), split at every unescaped `|`; outer pipes are optionalstatic rowCells = (s) => {let a = 0let b = s.lengthwhile (a < b && isSpaceC(s.charCodeAt(a))) { a = a + 1 }while (b > a && isSpaceC(s.charCodeAt(b - 1))) { b = b - 1 }if (a < b && s.charCodeAt(a) == 124) { a = a + 1 }if (b > a && s.charCodeAt(b - 1) == 124 && !escapedAt(s, b - 1, a)) { b = b - 1 }cells = []let start = alet k = awhile (k < b) {if (s.charCodeAt(k) == 92 && k + 1 < b) {k = k + 2} else {if (s.charCodeAt(k) == 124) {cells.push(s.slice(start, k).trim())start = k + 1}k = k + 1}}cells.push(s.slice(start, b).trim())return cells}// a cell's Markdown: `\|` becomes `|`, every other escape stays for inlineSpansstatic unpipe = (t) => {let out = ''let k = 0while (k < t.length) {if (t.charCodeAt(k) == 92 && k + 1 < t.length) {out = out + (t.charCodeAt(k + 1) == 124 ? '|' : t.slice(k, k + 2))k = k + 2} else {out = out + t[k]k = k + 1}}return out}// one delimiter cell (`:?-+:?`) → '' | 'left' | 'center' | 'right', or null when it is not onestatic alignOfCell = (c) => {n = c.lengthl = n > 0 && c.charCodeAt(0) == 58a = l ? 1 : 0r = n > a && c.charCodeAt(n - 1) == 58b = r ? n - 1 : nif (b <= a) { return null }let k = awhile (k < b) {if (c.charCodeAt(k) != 45) { return null }k = k + 1}if (l && r) { return 'center' }if (r) { return 'right' }if (l) { return 'left' }return ''}// the delimiter row → its alignments, or nullstatic alignsOf = (line) => {if (indentOf(line) > 3 || !line.includes('|')) { return null }out = []for (c of rowCells(line)) {let al = alignOfCell(c)if (al == null) { return null }out.push(al)}return out}// a table starting at line i (a header row + a delimiter row with as many cells) → { aligns, head } or nullstatic tableAt = (lines, i) => {if (i + 1 >= lines.length) { return null }head = noCr(lines[i])if (indentOf(head) > 3 || head.trim() == '') { return null }aligns = alignsOf(noCr(lines[i + 1]))if (aligns == null) { return null }cells = rowCells(head)if (cells.length != aligns.length) { return null }return { aligns = aligns head = cells }}// a row's cells → [{ spans, align }], exactly as many as the table has columnsstatic tableRow = (cells, aligns) => {out = []let k = 0while (k < aligns.length) {let src = k < cells.length ? unpipe(cells[k]) : ''let spans = inlineSpans(src)out.push({ spans = spans align = aligns[k] })k = k + 1}return out}static tableBlock = (head, rows) => {return { isP = false isH1 = false isH2 = false isH3 = false isUl = false isOl = false isCode = false isTable = true spans = [] items = [] start = 1 code = '' head = head rows = rows }}static paraBlock = (lines) => {let spans = inlineSpans(lines.join(NL))return { isP = true isH1 = false isH2 = false isH3 = false isUl = false isOl = false isCode = false spans = spans items = [] start = 1 code = '' }}static headingBlock = (level, text) => {let spans = inlineSpans(text)return { isP = false isH1 = level == 1 isH2 = level == 2 isH3 = level >= 3 isUl = false isOl = false isCode = false spans = spans items = [] start = 1 code = '' }}static codeBlock = (lines) => {text = lines.join(NL)return { isP = false isH1 = false isH2 = false isH3 = false isUl = false isOl = false isCode = true spans = [] items = [] start = 1 code = text }}static listBlock = (ordered, first, itemLines) => {let items = []for (lines of itemLines) {let spans = inlineSpans(lines.join(NL))items.push({ spans = spans })}return { isP = false isH1 = false isH2 = false isH3 = false isUl = !ordered isOl = ordered isCode = false spans = [] items = items start = first code = '' }}// the Markdown text → blocks (see the header)static markdownBlocks = (text) => {out = []if (text == null || text == '') { return out }lines = ('' + text).split(NL)let para = []let inList = falselet listOrdered = falselet listStart = 1let items = []let blankBefore = falselet i = 0while (i < lines.length) {let line = noCr(lines[i])let fence = fenceOf(line)let h = fence == null ? headingOf(line) : nulllet it = fence == null && h == null ? itemOf(line) : nulllet tb = fence == null && h == null && it == null ? tableAt(lines, i) : nullif (fence != null) {if (para.length > 0) { out.push(paraBlock(para)) }para = []if (inList) { out.push(listBlock(listOrdered, listStart, items)) }inList = falseitems = []let code = []let j = i + 1let closed = falselet fc = fence.charCodeAt(0)while (j < lines.length && !closed) {let l = noCr(lines[j])let f = fenceOf(l)if (f != null && f.charCodeAt(0) == fc && f.length >= fence.length && l.trim() == f) {closed = true} else {code.push(l)}j = j + 1}out.push(codeBlock(code))i = jblankBefore = false} else if (line.trim() == '') {if (para.length > 0) { out.push(paraBlock(para)) }para = []blankBefore = truei = i + 1} else if (h != null) {if (para.length > 0) { out.push(paraBlock(para)) }para = []if (inList) { out.push(listBlock(listOrdered, listStart, items)) }inList = falseitems = []out.push(headingBlock(h.level, h.text))blankBefore = falsei = i + 1} else if (tb != null) {// a table: the header, the delimiter row, then every line up to a blank line or another blockif (para.length > 0) { out.push(paraBlock(para)) }para = []if (inList) { out.push(listBlock(listOrdered, listStart, items)) }inList = falseitems = []let head = tableRow(tb.head, tb.aligns)let rows = []let j = i + 2let more = truewhile (more && j < lines.length) {let l = noCr(lines[j])if (l.trim() == '' || fenceOf(l) != null || headingOf(l) != null || itemOf(l) != null) {more = false} else {rows.push({ cells = tableRow(rowCells(l), tb.aligns) })j = j + 1}}out.push(tableBlock(head, rows))i = jblankBefore = false} else if (it != null) {if (para.length > 0) { out.push(paraBlock(para)) }para = []if (inList && listOrdered != it.ordered) {out.push(listBlock(listOrdered, listStart, items))inList = falseitems = []}if (!inList) {inList = truelistOrdered = it.orderedlistStart = it.startitems = []}items.push([it.text])blankBefore = falsei = i + 1} else if (inList && (!blankBefore || indentOf(line) >= 2)) {// a wrapped item line (or an indented paragraph of the item): onto the last itemlet last = items[items.length - 1]last.push(line.trim())items[items.length - 1] = lastblankBefore = falsei = i + 1} else {if (inList) { out.push(listBlock(listOrdered, listStart, items)) }inList = falseitems = []para.push(line.trim())blankBefore = falsei = i + 1}}if (para.length > 0) { out.push(paraBlock(para)) }if (inList) { out.push(listBlock(listOrdered, listStart, items)) }return out}
Branches
Latest commits
- 81f10e4eident selector back in the header (creator 2026-10-24): selector top right signed in and out on phone and desktop, no second Log in button, new gate checks (WIP: gates not re-run after the last edits)mre
- f51029abMerge branch 'sy-sync'mre
- eae6e8c7sync components.hybriel 4922f9c: comments like / Edit / Delete / 'edited' are 44 px targets; gate: 'edited' hit-area padding, thread targets check (SY)mre
- 0de4750btheme: sync from layouts 4994037 (list rows stack, td word-keep, heading code, edited tap target)mre
- 4eeb7bfeST2 report wordingmre
- 2558f56cst2-sec: keep the hlsid session cookie name (no one is signed out), Secure flag only; gate: a cookie issued by main stays signed in; ident stub serves selector.js; security.mjs in deploy GATESmre
- ea2e440esecurity audit (ST): JSON body cap, relation rights, login session rotation, __Host- Secure cookie, API security headers, CSP+referrer meta, /connect cap, one-line subjects, tests/security.mjs, reportmre
- 7c8c2282sessions last 99 years (creator 2026-10-23: logins never expire; only sign out ends them)mre
- b6277497report MT-mobile, LOGmre
- 6709398blist paging: the newest rows come first (the sorted array was not used); filter rows hide their scrollbar; gate checks the ordermre
- 31736a80gate: member-name check fails on the old row layout toomre
- e03ae537gate: the phone checks run on without the Show more button (so old code reports every failed check)mre
- adf7f132gate: 390 px checks for the mobile audit fixes (row number, filters, field widths, Edit, member names, relation taps, paging)mre
- 28e9c938ticket list: draw the newest 50 rows and a Show more button (500 rows were a 2-4 s first paint, 740 KB, a 90 000 px phone page)mre
- ccebb0c7mobile: ticket number stays on one line, filter chips one scrolling row, fields full width under their caption, Edit/action buttons, member names whole, 44 px relation linksmre
- f2dc7741gate: the phone toolbar is two even rows (E2)mre
- 20f7c33async components.hybriel 214c318: MdEditor on a phone, two even toolbar rows of 44 px targets, label 15 px, table head 16 px (E2)mre
- d74a5482sync components.hybriel 134b4e3: MdEditor builds an editor put into the page at once, redraws a surface put in anew (F5, hybriel#211)mre
- 9d02c540sync components.hybriel 6b0936a: the 'edited' foldable drops the theme's border and marginmre
- 22751914sync components.hybriel 8d79ff7: bubble hugs its content, quiet 'edited' label; gate checksmre