(
code: string,
language: string = "javascript",
currLineOffsetFromTop: number,
newDiffLines: DiffLine[],
)
| 167 | } |
| 168 | |
| 169 | async highlightCode( |
| 170 | code: string, |
| 171 | language: string = "javascript", |
| 172 | currLineOffsetFromTop: number, |
| 173 | newDiffLines: DiffLine[], |
| 174 | ): Promise<string> { |
| 175 | const lines = code.split("\n"); |
| 176 | const newDiffLineMap = new Set(); |
| 177 | |
| 178 | if (newDiffLines) { |
| 179 | newDiffLines.forEach((diffLine) => { |
| 180 | if (diffLine.type === "new") { |
| 181 | newDiffLineMap.add(diffLine.line); |
| 182 | } |
| 183 | }); |
| 184 | } |
| 185 | |
| 186 | const annotatedLines = []; |
| 187 | |
| 188 | // NOTE: Shiki's preprocessor deletes transformer annotations when applied to an empty line. |
| 189 | // If you are transforming an empty line, make sure that |
| 190 | // the transformation is applied to a non-empty line first. |
| 191 | for (let i = 0; i < lines.length; i++) { |
| 192 | const line = lines[i]; |
| 193 | |
| 194 | // Add highlight comment before target line. |
| 195 | if (i + 1 === currLineOffsetFromTop && currLineOffsetFromTop >= 0) { |
| 196 | annotatedLines.push("// [!code highlight:1]"); |
| 197 | } |
| 198 | |
| 199 | // Handle diff lines |
| 200 | if (newDiffLineMap.has(line)) { |
| 201 | if (line.trim() === "") { |
| 202 | // For empty lines, add the magic comment on a separate line before. |
| 203 | annotatedLines.push("// [!code ++]"); |
| 204 | annotatedLines.push(line); // The empty line itself. |
| 205 | } else { |
| 206 | // For non-empty lines, append the magic comment. |
| 207 | annotatedLines.push(line + "// [!code ++]"); |
| 208 | } |
| 209 | newDiffLineMap.delete(line); |
| 210 | } else { |
| 211 | annotatedLines.push(line); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | const annotatedCode = annotatedLines.join("\n"); |
| 216 | |
| 217 | await this.highlighter!.loadLanguage(language as BundledLanguage); |
| 218 | return this.highlighter!.codeToHtml(annotatedCode, { |
| 219 | lang: language, |
| 220 | theme: this.currentTheme, |
| 221 | transformers: [transformerNotationHighlight(), transformerNotationDiff()], |
| 222 | }); |
| 223 | } |
| 224 | |
| 225 | async convertToSVG( |
| 226 | code: string, |
no test coverage detected