| 1 | package markup |
| 2 | |
| 3 | import ( |
| 4 | "html" |
| 5 | "strings" |
| 6 | ) |
| 7 | |
| 8 | // Chips are how a [[link]] and an embed are drawn (spec §4.2): a link |
| 9 | // whose mapa-chip names its kind, a glyph, the text as typed, and an |
| 10 | // accessible name that says what the chip is. editor/chips.mjs draws the |
| 11 | // same markup; gate L4 holds them together. |
| 12 | // |
| 13 | // team: <a href mapa-chip="team" aria-label="X, team page"> |
| 14 | // alias: <a href mapa-chip="team" title="Now called Y" |
| 15 | // aria-label="X, now called Y"> |
| 16 | // pending: <a href mapa-chip="pending" |
| 17 | // aria-label="X, page not written yet"> |
| 18 | // date: <a href mapa-chip="date" aria-label="Thursday 12 |
| 19 | // November, your note"> |
| 20 | // block: <a href mapa-chip="block" aria-label="X, a block in Y"> |
| 21 | // gone block: <a href mapa-chip="block gone" aria-label="X, block |
| 22 | // removed from Y"> ... <span mapa-chip-note> |
| 23 | // broken: <span mapa-chip="broken">2026-02-30</span> |
| 24 | // embed: <span mapa-chip="embed" role="img" |
| 25 | // aria-label="app:item, embedded item, not shown yet"> |
| 26 | |
| 27 | // glyphs are the chips' icons, Lucide's strokes on a 24px grid. |
| 28 | var glyphs = map[string][]string{ |
| 29 | "page": {"M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 " + |
| 30 | "2-2V7Z", "M14 2v4a2 2 0 0 0 2 2h4", "M9 13h6", "M9 17h4"}, |
| 31 | "page-plus": {"M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 " + |
| 32 | "2-2V7Z", "M14 2v4a2 2 0 0 0 2 2h4", "M9 15h6", "M12 12v6"}, |
| 33 | "calendar": {"M8 2v4", "M16 2v4", "M5 4h14a2 2 0 0 1 2 2v14a2 2 0 0 " + |
| 34 | "1-2 2H5a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2Z", "M3 10h18"}, |
| 35 | "block": {"M13 4v16", "M17 4v16", "M19 4H9.5a4.5 4.5 0 0 0 0 9H13"}, |
| 36 | "embed": {"M21 8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 " + |
| 37 | "8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16Z", |
| 38 | "m3.3 7 8.7 5 8.7-5", "M12 22V12"}, |
| 39 | } |
| 40 | |
| 41 | func glyph(name string) string { |
| 42 | var out strings.Builder |
| 43 | out.WriteString(`<svg mapa-i viewBox="0 0 24 24" aria-hidden="true">`) |
| 44 | for _, path := range glyphs[name] { |
| 45 | out.WriteString(`<path d="` + path + `"></path>`) |
| 46 | } |
| 47 | out.WriteString("</svg>") |
| 48 | return out.String() |
| 49 | } |
| 50 | |
| 51 | // chip is one link chip's parts. |
| 52 | type chip struct { |
| 53 | href, kind, title, label, glyph, shown, note string |
| 54 | icon ChipIcon |
| 55 | } |
| 56 | |
| 57 | // iconHTML is a page's icon in a chip's glyph cell (columns and icons |
| 58 | // spec §2.4): decoration, so aria-hidden and alt="". editor/chips.mjs |
| 59 | // draws the same. |
| 60 | func iconHTML(icon ChipIcon) string { |
| 61 | if icon.Emoji != "" { |
| 62 | return `<span mapa-i mapa-icon aria-hidden="true">` + |
| 63 | html.EscapeString(icon.Emoji) + "</span>" |
| 64 | } |
| 65 | return `<img mapa-i mapa-icon src="` + ImagePrefix + icon.Image + |
| 66 | `" alt width="16" height="16" loading="lazy" decoding="async">` |
| 67 | } |
| 68 | |
| 69 | func (parts chip) html() string { |
| 70 | out := `<a href="` + html.EscapeString(parts.href) + `" mapa-chip="` + |
| 71 | parts.kind + `"` |
| 72 | if parts.title != "" { |
| 73 | out += ` title="` + html.EscapeString(parts.title) + `"` |
| 74 | } |
| 75 | drawn := glyph(parts.glyph) |
| 76 | if parts.icon.Emoji != "" || IsImageID(parts.icon.Image) { |
| 77 | drawn = iconHTML(parts.icon) |
| 78 | } |
| 79 | return out + ` aria-label="` + html.EscapeString(parts.label) + `">` + |
| 80 | drawn + html.EscapeString(parts.shown) + parts.note + "</a>" |
| 81 | } |
| 82 | |
| 83 | // wikiChip draws a [[link]] as the person reading sees it. |
| 84 | func wikiChip(span Span, resolver Resolver) string { |
| 85 | answer := resolver.Resolve(span.Target, span.Anchor) |
| 86 | shown := firstOf(span.Text, span.Target) |
| 87 | switch { |
| 88 | case answer.Broken: |
| 89 | return `<span mapa-chip="broken">` + html.EscapeString(shown) + |
| 90 | "</span>" |
| 91 | case answer.Day != "": |
| 92 | return dateChip(span, answer) |
| 93 | case answer.Pending: |
| 94 | return chip{href: answer.Href, kind: "pending", glyph: "page-plus", |
| 95 | label: shown + ", page not written yet", shown: shown}.html() |
| 96 | } |
| 97 | title := firstOf(answer.Title, span.Target) |
| 98 | parts := chip{href: answer.Href, kind: "team", glyph: "page", |
| 99 | label: shown + ", team page", shown: shown, icon: answer.Icon} |
| 100 | if answer.Alias { |
| 101 | parts.title = "Now called " + title |
| 102 | parts.label = shown + ", now called " + title |
| 103 | } |
| 104 | if span.Anchor != "" { |
| 105 | parts.kind, parts.glyph, parts.icon = "block", "block", ChipIcon{} |
| 106 | parts.label = shown + ", a block in " + title |
| 107 | } |
| 108 | if span.Anchor != "" && answer.BlockGone { |
| 109 | parts.kind = "block gone" |
| 110 | parts.label = shown + ", block removed from " + title |
| 111 | parts.note = "<span mapa-chip-note>block removed</span>" |
| 112 | } |
| 113 | return parts.html() |
| 114 | } |
| 115 | |
| 116 | // dateChip is a date link: the day in words, never the ISO date. |
| 117 | func dateChip(span Span, answer Resolution) string { |
| 118 | parts := chip{href: answer.Href, kind: "date", glyph: "calendar", |
| 119 | shown: answer.Day, label: answer.Day + ", your note"} |
| 120 | if span.Text != "" { |
| 121 | parts.shown = span.Text |
| 122 | parts.label = span.Text + ", your note for " + answer.Day |
| 123 | } |
| 124 | return parts.html() |
| 125 | } |
| 126 | |
| 127 | // embedChip is slice 1's embed: a quiet chip showing the reference. |
| 128 | func embedChip(span Span) string { |
| 129 | ref := strings.TrimSuffix(strings.TrimPrefix(span.Text, "![["), "]]") |
| 130 | return `<span mapa-chip="embed" role="img" aria-label="` + |
| 131 | html.EscapeString(ref+", embedded item, not shown yet") + `">` + |
| 132 | glyph("embed") + html.EscapeString(ref) + "</span>" |
| 133 | } |
| 134 | |