|
4 | 4 | * Copyright (C) 2020-2022 Posit Software, PBC |
5 | 5 | * |
6 | 6 | */ |
7 | | -import { assert } from "testing/asserts"; |
| 7 | +import { assert, assertEquals } from "testing/asserts"; |
8 | 8 | import { Metadata } from "../../src/config/types.ts"; |
9 | | -import { languagesWithClasses, partitionMarkdown } from "../../src/core/pandoc/pandoc-partition.ts"; |
| 9 | +import { languagesWithClasses, markdownWithExtractedHeading, partitionMarkdown } from "../../src/core/pandoc/pandoc-partition.ts"; |
10 | 10 | import { unitTest } from "../test.ts"; |
11 | 11 |
|
12 | 12 | // deno-lint-ignore require-await |
@@ -73,3 +73,149 @@ y = 2 |
73 | 73 | assert(result.has("python"), "Should have language 'python'"); |
74 | 74 | assert(result.get("python") === "foo", "python should have class 'foo'"); |
75 | 75 | }); |
| 76 | + |
| 77 | +// deno-lint-ignore require-await |
| 78 | +unitTest( |
| 79 | + "markdownWithExtractedHeading - ignores an ATX-heading-like line inside a fenced code block", |
| 80 | + async () => { |
| 81 | + const markdown = [ |
| 82 | + "```{python}", |
| 83 | + "print('hello')", |
| 84 | + "# plt.savefig('x.svg')", |
| 85 | + "```", |
| 86 | + ].join("\n"); |
| 87 | + const result = markdownWithExtractedHeading(markdown); |
| 88 | + assertEquals( |
| 89 | + result.headingText, |
| 90 | + undefined, |
| 91 | + "A comment inside a fenced code block must not be treated as a heading", |
| 92 | + ); |
| 93 | + }, |
| 94 | +); |
| 95 | + |
| 96 | +// deno-lint-ignore require-await |
| 97 | +unitTest( |
| 98 | + "markdownWithExtractedHeading - still extracts a real heading after a closed fenced code block", |
| 99 | + async () => { |
| 100 | + const markdown = [ |
| 101 | + "```{python}", |
| 102 | + "print('hello')", |
| 103 | + "```", |
| 104 | + "", |
| 105 | + "# Real Heading", |
| 106 | + ].join("\n"); |
| 107 | + const result = markdownWithExtractedHeading(markdown); |
| 108 | + assertEquals(result.headingText, "Real Heading"); |
| 109 | + assert( |
| 110 | + result.contentBeforeHeading, |
| 111 | + "The fenced block content should still count as content before the heading", |
| 112 | + ); |
| 113 | + }, |
| 114 | +); |
| 115 | + |
| 116 | +// deno-lint-ignore require-await |
| 117 | +unitTest( |
| 118 | + "markdownWithExtractedHeading - still promotes a heading that follows a prose paragraph (no fence)", |
| 119 | + async () => { |
| 120 | + const markdown = [ |
| 121 | + "Some intro paragraph.", |
| 122 | + "", |
| 123 | + "# Real Heading", |
| 124 | + ].join("\n"); |
| 125 | + const result = markdownWithExtractedHeading(markdown); |
| 126 | + assertEquals( |
| 127 | + result.headingText, |
| 128 | + "Real Heading", |
| 129 | + "Non-fenced prose before a heading must not prevent heading extraction", |
| 130 | + ); |
| 131 | + }, |
| 132 | +); |
| 133 | + |
| 134 | +// deno-lint-ignore require-await |
| 135 | +unitTest( |
| 136 | + "markdownWithExtractedHeading - ignores an ATX-heading-like line inside a tilde-fenced code block", |
| 137 | + async () => { |
| 138 | + const markdown = [ |
| 139 | + "~~~python", |
| 140 | + "# not a heading", |
| 141 | + "~~~", |
| 142 | + ].join("\n"); |
| 143 | + const result = markdownWithExtractedHeading(markdown); |
| 144 | + assertEquals(result.headingText, undefined); |
| 145 | + }, |
| 146 | +); |
| 147 | + |
| 148 | +// deno-lint-ignore require-await |
| 149 | +unitTest( |
| 150 | + "markdownWithExtractedHeading - ignores an ATX-heading-like line inside a fenced code block whose fence markers are indented up to 3 spaces", |
| 151 | + async () => { |
| 152 | + const markdown = [ |
| 153 | + "- a list item with a nested code block", |
| 154 | + "", |
| 155 | + " ```python", |
| 156 | + "# not a heading", |
| 157 | + " ```", |
| 158 | + ].join("\n"); |
| 159 | + const result = markdownWithExtractedHeading(markdown); |
| 160 | + assertEquals(result.headingText, undefined); |
| 161 | + }, |
| 162 | +); |
| 163 | + |
| 164 | +// deno-lint-ignore require-await |
| 165 | +unitTest( |
| 166 | + "markdownWithExtractedHeading - closes a fence whose closing marker is indented, then extracts the following heading", |
| 167 | + async () => { |
| 168 | + const markdown = [ |
| 169 | + "```python", |
| 170 | + "code", |
| 171 | + " ```", |
| 172 | + "# Real Heading", |
| 173 | + ].join("\n"); |
| 174 | + const result = markdownWithExtractedHeading(markdown); |
| 175 | + assertEquals(result.headingText, "Real Heading"); |
| 176 | + }, |
| 177 | +); |
| 178 | + |
| 179 | +// deno-lint-ignore require-await |
| 180 | +unitTest( |
| 181 | + "markdownWithExtractedHeading - extracts a setext-style heading", |
| 182 | + async () => { |
| 183 | + const markdown = [ |
| 184 | + "Real Heading", |
| 185 | + "===", |
| 186 | + ].join("\n"); |
| 187 | + const result = markdownWithExtractedHeading(markdown); |
| 188 | + assertEquals(result.headingText, "Real Heading"); |
| 189 | + assertEquals(result.lines, []); |
| 190 | + }, |
| 191 | +); |
| 192 | + |
| 193 | +// deno-lint-ignore require-await |
| 194 | +unitTest( |
| 195 | + "markdownWithExtractedHeading - no heading present", |
| 196 | + async () => { |
| 197 | + const markdown = [ |
| 198 | + "Just a paragraph.", |
| 199 | + "", |
| 200 | + "Another paragraph.", |
| 201 | + ].join("\n"); |
| 202 | + const result = markdownWithExtractedHeading(markdown); |
| 203 | + assertEquals(result.headingText, undefined); |
| 204 | + assertEquals(result.lines, markdown.split("\n")); |
| 205 | + }, |
| 206 | +); |
| 207 | + |
| 208 | +// deno-lint-ignore require-await |
| 209 | +unitTest( |
| 210 | + "markdownWithExtractedHeading - contentBeforeHeading is false when the heading is the first line", |
| 211 | + async () => { |
| 212 | + const markdown = [ |
| 213 | + "# Real Heading", |
| 214 | + "", |
| 215 | + "Some body text.", |
| 216 | + ].join("\n"); |
| 217 | + const result = markdownWithExtractedHeading(markdown); |
| 218 | + assertEquals(result.headingText, "Real Heading"); |
| 219 | + assertEquals(result.contentBeforeHeading, false); |
| 220 | + }, |
| 221 | +); |
0 commit comments