(n: Node, o: Options = {})
| 141 | } |
| 142 | |
| 143 | function* formatNode(n: Node, o: Options = {}) { |
| 144 | const no = { ...o, parent: n }; |
| 145 | const indent = SPACE.repeat(no.indent || 0); |
| 146 | |
| 147 | switch (n.type) { |
| 148 | case 'document': { |
| 149 | if (n.attributes.frontmatter && n.attributes.frontmatter.length) { |
| 150 | yield '---' + NL + n.attributes.frontmatter + NL + '---' + NL + NL; |
| 151 | } |
| 152 | yield* trimStart(formatChildren(n, no)); |
| 153 | break; |
| 154 | } |
| 155 | case 'heading': { |
| 156 | yield NL; |
| 157 | yield indent; |
| 158 | yield '#'.repeat(n.attributes.level || 1); |
| 159 | yield SPACE; |
| 160 | yield* trimStart(formatChildren(n, no)); |
| 161 | yield* formatAnnotations(n); |
| 162 | yield NL; |
| 163 | break; |
| 164 | } |
| 165 | case 'paragraph': { |
| 166 | yield NL; |
| 167 | yield* formatChildren(n, no); |
| 168 | yield* formatAnnotations(n); |
| 169 | yield NL; |
| 170 | break; |
| 171 | } |
| 172 | case 'inline': { |
| 173 | yield indent; |
| 174 | yield* formatChildren(n, no); |
| 175 | break; |
| 176 | } |
| 177 | case 'image': { |
| 178 | yield '!'; |
| 179 | yield '['; |
| 180 | yield* formatValue(n.attributes.alt, no); |
| 181 | yield ']'; |
| 182 | yield '('; |
| 183 | yield* typeof n.attributes.src === 'string' |
| 184 | ? escapeMarkdownCharacters(n.attributes.src, /[()]/g) |
| 185 | : formatValue(n.attributes.src, no); |
| 186 | if (n.attributes.title) { |
| 187 | yield SPACE + `"${n.attributes.title}"`; |
| 188 | } |
| 189 | yield ')'; |
| 190 | break; |
| 191 | } |
| 192 | case 'link': { |
| 193 | const children = [...formatChildren(n, no)].join(''); |
| 194 | |
| 195 | // https://spec.commonmark.org/0.31.2/#autolinks |
| 196 | if (children === n.attributes.href && !n.attributes.title) { |
| 197 | yield `<${n.attributes.href}>`; |
| 198 | break; |
| 199 | } |
| 200 |
no test coverage detected
searching dependent graphs…