| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "sort" |
| 6 | |
| 7 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 8 | ) |
| 9 | |
| 10 | // Pages inside (columns and icons spec §5): a team page is inside team |
| 11 | // page P when its first block is exactly the line "In: [[P]]" (or |
| 12 | // "In: [[P|shown]]", with an optional ^id), the line the Notion import |
| 13 | // writes. The relation is derived from bodies in memory, like every |
| 14 | // link; nothing is stored. Only team pages are inside anything, and |
| 15 | // only team pages hold pages, so no private first line reaches the |
| 16 | // sidebar (level-up build note 80). |
| 17 | |
| 18 | // inTarget is the folded link text of a page's "In:" line, or "". |
| 19 | func inTarget(kind, rung string, blocks []markup.Block) string { |
| 20 | if kind != KindPage || rung != RungTeam || len(blocks) == 0 { |
| 21 | return "" |
| 22 | } |
| 23 | first := blocks[0] |
| 24 | if first.Kind != markup.Paragraph { |
| 25 | return "" |
| 26 | } |
| 27 | spans := markup.Spans(markup.Strip(first)) |
| 28 | if len(spans) != 2 || spans[0].Kind != markup.Text || |
| 29 | spans[0].Text != "In: " || spans[1].Kind != markup.WikiLink || |
| 30 | spans[1].Anchor != "" || markup.IsDateShaped(spans[1].Target) { |
| 31 | return "" |
| 32 | } |
| 33 | return Fold(spans[1].Target) |
| 34 | } |
| 35 | |
| 36 | // insideOf is the ids of the team pages inside page, unordered. The |
| 37 | // caller holds the read lock. |
| 38 | func (index *Index) insideOf(page *entry) map[int64]bool { |
| 39 | out := map[int64]bool{} |
| 40 | if page == nil || !page.team() { |
| 41 | return out |
| 42 | } |
| 43 | for _, fold := range page.titleFolds() { |
| 44 | for child := range index.within[fold] { |
| 45 | if child != page.id && index.team[child] { |
| 46 | out[child] = true |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | return out |
| 51 | } |
| 52 | |
| 53 | // insideOrder is page's children in its own order (§5.2): those its |
| 54 | // body links to, by their first link, then the rest by title. |
| 55 | func (index *Index) insideOrder(page *entry, |
| 56 | children map[int64]bool) []*entry { |
| 57 | place := map[int64]int{} |
| 58 | for at, link := range page.links { |
| 59 | target, ok := index.titles[Fold(link.Target)] |
| 60 | if _, seen := place[target]; ok && children[target] && !seen { |
| 61 | place[target] = at |
| 62 | } |
| 63 | } |
| 64 | out := make([]*entry, 0, len(children)) |
| 65 | for child := range children { |
| 66 | out = append(out, index.pages[child]) |
| 67 | } |
| 68 | sort.SliceStable(out, func(left, right int) bool { |
| 69 | one, okOne := place[out[left].id] |
| 70 | other, okOther := place[out[right].id] |
| 71 | switch { |
| 72 | case okOne && okOther: |
| 73 | return one < other |
| 74 | case okOne != okOther: |
| 75 | return okOne |
| 76 | } |
| 77 | return Fold(out[left].record.Title) < Fold(out[right].record.Title) |
| 78 | }) |
| 79 | return out |
| 80 | } |
| 81 | |
| 82 | // Inside is the pages inside page id as scope sees them, in order: the |
| 83 | // scoped accessor's answer (level-up §8). A page the scope may not open, |
| 84 | // or a private one, has none. |
| 85 | func (index *Index) Inside(ctx context.Context, scope Scope, |
| 86 | pageID int64) ([]*entry, error) { |
| 87 | if err := index.Sync(ctx); err != nil { |
| 88 | return nil, err |
| 89 | } |
| 90 | index.mutex.RLock() |
| 91 | defer index.mutex.RUnlock() |
| 92 | return index.insideLocked(scope, pageID), nil |
| 93 | } |
| 94 | |
| 95 | // holdsAny is whether any page the scope may open is inside page id: |
| 96 | // the arrow's test for a closed row, which needs no order. The caller |
| 97 | // holds the read lock. |
| 98 | func (index *Index) holdsAny(scope Scope, pageID int64) bool { |
| 99 | page := index.in(scope).page(pageID) |
| 100 | return page != nil && page.team() && len(index.insideOf(page)) > 0 |
| 101 | } |
| 102 | |
| 103 | func (index *Index) insideLocked(scope Scope, pageID int64) []*entry { |
| 104 | page := index.in(scope).page(pageID) |
| 105 | if page == nil || !page.team() { |
| 106 | return nil |
| 107 | } |
| 108 | return index.insideOrder(page, index.insideOf(page)) |
| 109 | } |
| 110 | |