| 1 | package notionmd |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | // LinkState is what a Notion id is to this import (§11.2). |
| 10 | type LinkState int |
| 11 | |
| 12 | // The states a mention, a link_to_page or a Notion URL can resolve to. |
| 13 | const ( |
| 14 | // Unshared: never seen by the import. Written as |
| 15 | // "(a Notion page not shared with the import)", plain, counted, and |
| 16 | // its id returned in Result.Unresolved so a later run can heal it. |
| 17 | Unshared LinkState = iota |
| 18 | // Live: imported and live in Mapa. Written as a [[link]] to the |
| 19 | // title the resolver gives, which is final for this run. |
| 20 | Live |
| 21 | // Dead: mapped, but deleted in Mapa or failed in this run. Its |
| 22 | // text, plain, counted (a deleted page's title is free). |
| 23 | Dead |
| 24 | // Row: a database row. Its text, plain, counted. |
| 25 | Row |
| 26 | // Database: a database. Its text, plain, counted. |
| 27 | Database |
| 28 | ) |
| 29 | |
| 30 | // Resolver answers what a Notion page or database id is, and its Mapa |
| 31 | // title when it is Live. Ids are compared without dashes, lower case. |
| 32 | type Resolver interface { |
| 33 | Page(notionID string) (title string, state LinkState) |
| 34 | } |
| 35 | |
| 36 | // Images is the image step's seam (§12). The run fetches each |
| 37 | // Notion-hosted image before the page is rendered and answers, for the |
| 38 | // image block's id, the line the body holds in its place: an image |
| 39 | // reference once Mapa has images, or the refusal line |
| 40 | // "(Image not imported: <file name>, <reason>)". The mapper writes it as |
| 41 | // one paragraph, held to the structure check like any other. |
| 42 | type Images interface { |
| 43 | Image(blockID string) string |
| 44 | } |
| 45 | |
| 46 | // Options are what a page's render needs from the run. |
| 47 | type Options struct { |
| 48 | // Context ends a render that a Stop or the run's per-page deadline |
| 49 | // has outrun: Render answers ErrRenderFailed with its error. |
| 50 | Context context.Context |
| 51 | Resolve Resolver |
| 52 | Images Images |
| 53 | // Zone is the team's time zone, for a dated mention with a time and |
| 54 | // no zone of its own (§11.2). |
| 55 | Zone *time.Location |
| 56 | // Parent is the parent's Mapa title, for the "In: [[Parent]]" line |
| 57 | // (§9.4); "" writes no line. |
| 58 | Parent string |
| 59 | // Date is set when the page's Notion title is date-shaped: the |
| 60 | // first line after the In: line is the date link [[Date]] (§9.3). |
| 61 | Date string |
| 62 | } |
| 63 | |
| 64 | // Result is a rendered page. |
| 65 | type Result struct { |
| 66 | Body string |
| 67 | // Losses counts what the subset could not say, by kind (Loss* |
| 68 | // constants, and LossBlock+type for blocks not imported). |
| 69 | Losses map[string]int `json:"losses,omitempty"` |
| 70 | // Placeholders is the Notion id behind each placeholder (§10.2). |
| 71 | Placeholders []string `json:"placeholders,omitempty"` |
| 72 | // Unresolved is each page id mentioned that was not shared (§13.5). |
| 73 | Unresolved []string `json:"unresolved,omitempty"` |
| 74 | // Targets is each Notion id written as a [[link]], so the fix-up |
| 75 | // pass can find pages linking to one whose outcome changed (§13.6). |
| 76 | Targets []string `json:"targets,omitempty"` |
| 77 | // NotionLinks is each link still pointing at Notion (§11.3). |
| 78 | NotionLinks int `json:"notionLinks,omitempty"` |
| 79 | // Fallbacks counts blocks written as a fenced copy (§11.4), and |
| 80 | // Neutralised the U+200B and look-alike insertions. |
| 81 | Fallbacks int `json:"fallbacks,omitempty"` |
| 82 | Neutralised int `json:"neutralised,omitempty"` |
| 83 | } |
| 84 | |
| 85 | // ErrRenderFailed is a page whose fallback itself failed the structure |
| 86 | // check: the run records render_failed and writes nothing (§11.4). |
| 87 | var ErrRenderFailed = errors.New("notionmd: render_failed") |
| 88 | |
| 89 | // Loss kinds. The report labels each (import_report.go). |
| 90 | const ( |
| 91 | LossBlock = "block:" // + the Notion type: a block not imported |
| 92 | LossToggle = "toggle" |
| 93 | LossColumns = "columns" |
| 94 | LossWidths = "column_widths" |
| 95 | LossTabs = "tabs" |
| 96 | LossSynced = "synced" |
| 97 | LossIndent = "indent" |
| 98 | LossNesting = "nesting" |
| 99 | LossHoisted = "hoisted" |
| 100 | LossBlankLines = "blank_lines" |
| 101 | LossColor = "color" |
| 102 | LossStrike = "strikethrough" |
| 103 | LossUnderline = "underline" |
| 104 | LossCodeTick = "code_backtick" |
| 105 | LossMarkStar = "mark_star" |
| 106 | LossMarkLink = "mark_link" |
| 107 | LossCodeFence = "code_fence" |
| 108 | LossFile = "file" |
| 109 | LossStartNumber = "start_number" |
| 110 | LossBlockAnchor = "block_anchor" |
| 111 | LossUnsafeLink = "unsafe_link" |
| 112 | LossLongLink = "long_link" |
| 113 | LossUnshared = "unshared_link" |
| 114 | LossDeadLink = "dead_link" |
| 115 | LossRowLink = "row_link" |
| 116 | LossDatabase = "database" |
| 117 | LossTableLayout = "table_layout" |
| 118 | LossCallout = "callout_icon" |
| 119 | LossMeeting = "meeting_notes" |
| 120 | ) |
| 121 | |
| 122 | // Render maps one page. It never drops a block silently: what the |
| 123 | // subset cannot say is a placeholder or a counted loss. It returns |
| 124 | // ErrRenderFailed only when a block's fallback fails the check too. |
| 125 | func Render(page Page, options Options) (Result, error) { |
| 126 | return render(page, options) |
| 127 | } |
| 128 | |