| 1 | package markup |
| 2 | |
| 3 | import "strings" |
| 4 | |
| 5 | // The parser's own predicates, exported for the Notion import's mapper |
| 6 | // (internal/mapa/notionmd, import spec §11.4). Mapa has no escapes, so |
| 7 | // the mapper neutralises literal text that would otherwise be read as |
| 8 | // markup, and it must ask the parser what would be read rather than keep |
| 9 | // a second copy of these patterns: a copy drifts from the parser it is |
| 10 | // meant to agree with. |
| 11 | |
| 12 | // StartsBlock is whether line, as the first or a later line of a block, |
| 13 | // would start a block of its own: a fence, heading, rule, list item or |
| 14 | // quote. |
| 15 | func StartsBlock(line string) bool { return startsBlock(line) } |
| 16 | |
| 17 | // MarkerAt is the byte index of the ^ of a trailing ^id on line, or -1: |
| 18 | // where a block's marker would be read (§3.2). |
| 19 | func MarkerAt(line string) int { |
| 20 | match := markerTail.FindStringSubmatchIndex(line) |
| 21 | if match == nil { |
| 22 | return -1 |
| 23 | } |
| 24 | return match[2] - 1 |
| 25 | } |
| 26 | |
| 27 | // TaskBox is a list item's task box at the start of its text: the state |
| 28 | // inside the brackets and the box's width with its spacing, or "" and 0 |
| 29 | // when text has none. |
| 30 | func TaskBox(text string) (string, int) { |
| 31 | match := taskBox.FindStringSubmatchIndex(text) |
| 32 | if match == nil { |
| 33 | return "", 0 |
| 34 | } |
| 35 | return text[match[2]:match[3]], match[1] |
| 36 | } |
| 37 | |
| 38 | // ClosesFence is whether line closes a code block opened with fence: |
| 39 | // the closing test trims every leading space. |
| 40 | func ClosesFence(line, fence string) bool { |
| 41 | return strings.HasPrefix(strings.TrimLeft(line, " "), fence) |
| 42 | } |
| 43 | |