|
1 | 1 | import type { ShikijiTransformer } from 'shikiji'
|
2 | 2 | import { addClassToHast } from 'shikiji'
|
| 3 | +import type { Element } from 'hast' |
3 | 4 |
|
4 | 5 | export interface TransformerRenderWhitespaceOptions {
|
5 | 6 | /**
|
@@ -44,38 +45,45 @@ export function transformerRenderWhitespace(
|
44 | 45 |
|
45 | 46 | return {
|
46 | 47 | name: 'shikiji-transformers:render-whitespace',
|
47 |
| - line(node) { |
48 |
| - const first = node.children[0] |
49 |
| - if (!first || first.type !== 'element') |
50 |
| - return |
51 |
| - const textNode = first.children[0] |
52 |
| - if (!textNode || textNode.type !== 'text') |
53 |
| - return |
54 |
| - node.children = node.children.flatMap((token) => { |
55 |
| - if (token.type !== 'element') |
56 |
| - return token |
57 |
| - const node = token.children[0] |
58 |
| - if (node.type !== 'text' || !node.value) |
59 |
| - return token |
| 48 | + root(root) { |
| 49 | + const pre = root.children[0] as Element |
| 50 | + const code = pre.children[0] as Element |
| 51 | + code.children.forEach((line) => { |
| 52 | + if (line.type !== 'element') |
| 53 | + return |
| 54 | + const first = line.children[0] |
| 55 | + if (!first || first.type !== 'element') |
| 56 | + return |
| 57 | + const textNode = first.children[0] |
| 58 | + if (!textNode || textNode.type !== 'text') |
| 59 | + return |
| 60 | + line.children = line.children.flatMap((token) => { |
| 61 | + if (token.type !== 'element') |
| 62 | + return token |
| 63 | + const node = token.children[0] |
| 64 | + if (node.type !== 'text' || !node.value) |
| 65 | + return token |
60 | 66 |
|
61 |
| - // Split by whitespaces |
62 |
| - const parts = node.value.split(/([ \t])/).filter(i => i.length) |
63 |
| - if (parts.length <= 1) |
64 |
| - return token |
| 67 | + // Split by whitespaces |
| 68 | + const parts = node.value.split(/([ \t])/).filter(i => i.length) |
| 69 | + if (parts.length <= 1) |
| 70 | + return token |
65 | 71 |
|
66 |
| - return parts.map((part) => { |
67 |
| - const clone = { |
68 |
| - ...token, |
69 |
| - properties: { ...token.properties }, |
70 |
| - } |
71 |
| - clone.children = [{ type: 'text', value: part }] |
72 |
| - if (keys.includes(part)) { |
73 |
| - addClassToHast(clone, classMap[part]) |
74 |
| - delete clone.properties.style |
75 |
| - } |
76 |
| - return clone |
| 72 | + return parts.map((part) => { |
| 73 | + const clone = { |
| 74 | + ...token, |
| 75 | + properties: { ...token.properties }, |
| 76 | + } |
| 77 | + clone.children = [{ type: 'text', value: part }] |
| 78 | + if (keys.includes(part)) { |
| 79 | + addClassToHast(clone, classMap[part]) |
| 80 | + delete clone.properties.style |
| 81 | + } |
| 82 | + return clone |
| 83 | + }) |
77 | 84 | })
|
78 |
| - }) |
| 85 | + }, |
| 86 | + ) |
79 | 87 | },
|
80 | 88 | }
|
81 | 89 | }
|
0 commit comments