( decorations: Decoration[], recordMap: RecordMap )
| 2 | import { formatDate, getBlockValue, getTextContent } from 'notion-utils' |
| 3 | |
| 4 | export function decorationsToMarkdown( |
| 5 | decorations: Decoration[], |
| 6 | recordMap: RecordMap |
| 7 | ): string { |
| 8 | return decorations |
| 9 | .map((decoration) => { |
| 10 | const text = decoration[0] |
| 11 | const formatting = decoration[1] |
| 12 | |
| 13 | if (!formatting?.length) { |
| 14 | return text |
| 15 | } |
| 16 | |
| 17 | return formatting.reduce((result: string, format) => { |
| 18 | const formatType = format[0] |
| 19 | const formatValue = format[1] |
| 20 | |
| 21 | switch (formatType) { |
| 22 | case 'b': |
| 23 | return `**${result}**` |
| 24 | |
| 25 | case 'i': |
| 26 | return `_${result}_` |
| 27 | |
| 28 | case 's': |
| 29 | return `~~${result}~~` |
| 30 | |
| 31 | case 'c': |
| 32 | return `\`${result}\`` |
| 33 | |
| 34 | case 'a': |
| 35 | return `[${result}](${formatValue as string})` |
| 36 | |
| 37 | case 'p': { |
| 38 | const pageId = formatValue as string |
| 39 | const pageBlock = getBlockValue(recordMap.block[pageId]) |
| 40 | const pageTitle = (pageBlock as any)?.properties?.title |
| 41 | ? getTextContent((pageBlock as any).properties.title) |
| 42 | : pageId |
| 43 | return `[${pageTitle}](/${pageId})` |
| 44 | } |
| 45 | |
| 46 | case '‣': { |
| 47 | const [_, linkId] = formatValue as [string, string] |
| 48 | const targetBlock = getBlockValue(recordMap.block[linkId]) |
| 49 | const title = (targetBlock as any)?.properties?.title |
| 50 | ? getTextContent((targetBlock as any).properties.title) |
| 51 | : linkId |
| 52 | return `[${title}](/${linkId})` |
| 53 | } |
| 54 | |
| 55 | case 'e': |
| 56 | return `$${formatValue as string}$` |
| 57 | |
| 58 | case 'd': { |
| 59 | const dateVal = formatValue as FormattedDate |
| 60 | let dateStr = formatDate(dateVal.start_date) |
| 61 | if (dateVal.end_date) { |
no test coverage detected