oficina / base Public

Clone
git clone https://amadan.net/oficina/base

Plain git — no account needed to clone.

Download

Download this file

1// Copied from Equipo's EquipoKit (oficina/equipo 00d09aa7,
2// native/apple/Sources/EquipoKit/Markdown/MarkdownPresentation.swift). Base's changes are
3// marked "Base:"; keep the rest in step with Equipo's.
4import Foundation
5
6/// A flat list of layout rows. Base: an ordered list counts from its start. Depth changes indentation, never view nesting.
7/// Indentation stops at four levels so deeply nested replies remain readable
8/// at phone width; every item and all of its content are still present.
9enum MarkdownPresentation {
10 static func pasteLabel(_ node: ChatMarkdownNode) -> String { node.label ?? BaseText.text("chat.paste.text") }
11 struct Row: Identifiable {
12 var node: ChatMarkdownNode
13 var indentation: Int
14 var marker: String?
15 var id: Int { node.sourceOffset }
16 }
17 static func groups(_ nodes: [ChatMarkdownNode]) -> [ChatMarkdownNode] {
18 var out: [ChatMarkdownNode] = []
19 for node in nodes {
20 if ["text", "emphasis", "strong", "link", "code", "break"].contains(node.kind) {
21 if out.last?.kind == "inlineGroup" { out[out.count - 1].children.append(node) }
22 else { out.append(ChatMarkdownNode(sourceOffset: node.sourceOffset, kind: "inlineGroup", children: [node])) }
23 } else { out.append(node) }
24 }
25 return out
26 }
27 static func rows(_ nodes: [ChatMarkdownNode]) -> [Row] {
28 var stack = groups(nodes).reversed().map { Row(node: $0, indentation: 0, marker: nil) }
29 var out: [Row] = []
30 while let row = stack.popLast() {
31 if ["unordered", "ordered"].contains(row.node.kind) {
32 for (index, item) in row.node.children.enumerated().reversed() {
33 let children = groups(item.children)
34 for (offset, child) in children.enumerated().reversed() {
35 stack.append(Row(node: child, indentation: row.indentation + 1,
36 marker: offset == 0 ? (row.node.kind == "ordered" ? "\((row.node.start ?? 1) + index)." : "•") : nil))
37 }
38 }
39 } else {
40 var visible = row
41 visible.indentation = min(4, max(0, row.indentation - 1))
42 out.append(visible)
43 }
44 }
45 return out
46 }
47}
48