|
5 | 5 | * |
6 | 6 | */ |
7 | 7 |
|
| 8 | +import { stringify } from "encoding/yaml.ts"; |
8 | 9 | import { warning } from "log/mod.ts"; |
9 | 10 | import { |
10 | 11 | JupyterNotebook, |
11 | 12 | JupyterOutput, |
12 | 13 | JupyterToMarkdownOptions, |
13 | 14 | } from "./types.ts"; |
14 | 15 |
|
15 | | -function fixupBokehCells( |
16 | | - nb: JupyterNotebook, |
17 | | - _options: JupyterToMarkdownOptions, |
18 | | -): JupyterNotebook { |
| 16 | +import { kTitle } from "../../config/constants.ts"; |
| 17 | +import { Metadata } from "../../publish/netlify/api/index.ts"; |
| 18 | +import { lines } from "../lib/text.ts"; |
| 19 | +import { markdownWithExtractedHeading } from "../pandoc/pandoc-partition.ts"; |
| 20 | +import { partitionYamlFrontMatter, readYamlFromMarkdown } from "../yaml.ts"; |
| 21 | +import { JupyterNotebook, JupyterOutput } from "./types.ts"; |
| 22 | + |
| 23 | +function fixupBokehCells(nb: JupyterNotebook): JupyterNotebook { |
19 | 24 | for (const cell of nb.cells) { |
20 | 25 | if (cell.cell_type === "code") { |
21 | 26 | let needsFixup = false; |
@@ -101,19 +106,95 @@ function fixupBokehCells( |
101 | 106 | return nb; |
102 | 107 | } |
103 | 108 |
|
| 109 | +export function fixupFrontMatter(nb: JupyterNotebook): JupyterNotebook { |
| 110 | + // helper to generate yaml |
| 111 | + const asYamlText = (yaml: Metadata) => { |
| 112 | + return stringify(yaml, { |
| 113 | + indent: 2, |
| 114 | + lineWidth: -1, |
| 115 | + sortKeys: false, |
| 116 | + skipInvalid: true, |
| 117 | + }); |
| 118 | + }; |
| 119 | + |
| 120 | + // helper to create nb lines (w/ newline after) |
| 121 | + const nbLines = (lns: string[]) => { |
| 122 | + return lns.map((line) => `${line}\n`); |
| 123 | + }; |
| 124 | + |
| 125 | + // look for the first raw block that has a yaml object |
| 126 | + let partitioned: { yaml: string; markdown: string } | undefined; |
| 127 | + const frontMatterCellIndex = nb.cells.findIndex((cell) => { |
| 128 | + if (cell.cell_type === "raw") { |
| 129 | + partitioned = partitionYamlFrontMatter(cell.source.join("")) || undefined; |
| 130 | + return !!partitioned; |
| 131 | + } |
| 132 | + }); |
| 133 | + |
| 134 | + // if we have front matter and a title then we are done |
| 135 | + const yaml = partitioned ? readYamlFromMarkdown(partitioned.yaml) : undefined; |
| 136 | + if (yaml?.title) { |
| 137 | + return nb; |
| 138 | + } |
| 139 | + |
| 140 | + // snip the title out of the markdown |
| 141 | + let title: string | undefined; |
| 142 | + for (const cell of nb.cells) { |
| 143 | + if (cell.cell_type === "markdown") { |
| 144 | + const { lines, headingText } = markdownWithExtractedHeading( |
| 145 | + cell.source.join("\n"), |
| 146 | + ); |
| 147 | + if (headingText) { |
| 148 | + title = headingText; |
| 149 | + cell.source = nbLines(lines); |
| 150 | + break; |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + // if there is no title then we are done (the doc will have no title) |
| 156 | + if (!title) { |
| 157 | + return nb; |
| 158 | + } |
| 159 | + |
| 160 | + // if we have yaml then inject the title into the cell |
| 161 | + if (yaml) { |
| 162 | + // new yaml text with title |
| 163 | + yaml[kTitle] = title; |
| 164 | + const yamlText = asYamlText(yaml); |
| 165 | + |
| 166 | + // re-write cell |
| 167 | + const frontMatterCell = nb.cells[frontMatterCellIndex]; |
| 168 | + frontMatterCell.source = nbLines( |
| 169 | + lines(`---\n${yamlText}---\n\n${partitioned?.markdown || ""}`), |
| 170 | + ); |
| 171 | + |
| 172 | + // otherwise inject a new cell at the top |
| 173 | + } else { |
| 174 | + const yamlText = asYamlText({ title }); |
| 175 | + nb.cells.unshift({ |
| 176 | + cell_type: "raw", |
| 177 | + source: nbLines(lines(yamlText)), |
| 178 | + metadata: {}, |
| 179 | + }); |
| 180 | + } |
| 181 | + |
| 182 | + // return modified nb |
| 183 | + return nb; |
| 184 | +} |
| 185 | + |
104 | 186 | const fixups: (( |
105 | 187 | nb: JupyterNotebook, |
106 | | - options: JupyterToMarkdownOptions, |
107 | 188 | ) => JupyterNotebook)[] = [ |
108 | 189 | fixupBokehCells, |
| 190 | + fixupFrontMatter, |
109 | 191 | ]; |
110 | 192 |
|
111 | 193 | export function fixupJupyterNotebook( |
112 | 194 | nb: JupyterNotebook, |
113 | | - options: JupyterToMarkdownOptions, |
114 | 195 | ): JupyterNotebook { |
115 | 196 | for (const fixup of fixups) { |
116 | | - nb = fixup(nb, options); |
| 197 | + nb = fixup(nb); |
117 | 198 | } |
118 | 199 | return nb; |
119 | 200 | } |
0 commit comments