|
1 | | -import { codeToHighlightCodeHtml } from "./highlighter.ts"; |
2 | | - |
3 | 1 | export function mustUseAngularHighlighter(fileContent: string): boolean { |
4 | 2 | return ( |
5 | 3 | fileContent.includes("@angular/core") && fileContent.includes("template") |
6 | 4 | ); |
7 | 5 | } |
8 | | - |
9 | | -export async function highlightAngularComponent( |
10 | | - fileContent: string, |
11 | | -): Promise<string> { |
12 | | - const templateCode = getAngularTemplateCode(fileContent); |
13 | | - |
14 | | - let codeHighlighted; |
15 | | - if (templateCode) { |
16 | | - const componentWithEmptyTemplate = |
17 | | - removeAngularTemplateContent(fileContent); |
18 | | - const templateCodeHighlighted = await codeToHighlightCodeHtml( |
19 | | - templateCode, |
20 | | - "angular-html", |
21 | | - ); |
22 | | - |
23 | | - const componentWithoutTemplateHighlighted = await codeToHighlightCodeHtml( |
24 | | - componentWithEmptyTemplate, |
25 | | - "angular-ts", |
26 | | - ); |
27 | | - |
28 | | - codeHighlighted = componentWithoutTemplateHighlighted.replace( |
29 | | - "template", |
30 | | - "template: `" + removeCodeWrapper(templateCodeHighlighted) + "`,", |
31 | | - ); |
32 | | - } else { |
33 | | - codeHighlighted = await codeToHighlightCodeHtml(fileContent, "angular-ts"); |
34 | | - } |
35 | | - |
36 | | - return codeHighlighted; |
37 | | -} |
38 | | - |
39 | | -function getAngularTemplateCode(fileContent: string): string { |
40 | | - // regex to grab what is inside angular component template inside backticks |
41 | | - const regex = /template:\s*`([\s\S]*?)`/gm; |
42 | | - |
43 | | - // grab the template string |
44 | | - const template = regex.exec(fileContent); |
45 | | - |
46 | | - if (template) return template[1]; |
47 | | - |
48 | | - return ""; |
49 | | -} |
50 | | - |
51 | | -function removeAngularTemplateContent(fileContent: string): string { |
52 | | - const componentWithoutContentInsideTemplate = fileContent.replace( |
53 | | - /template:\s*`([\s\S]*?)([^*])`,?/gm, |
54 | | - "template", |
55 | | - ); |
56 | | - |
57 | | - return componentWithoutContentInsideTemplate; |
58 | | -} |
59 | | - |
60 | | -function removeCodeWrapper(html: string): string { |
61 | | - const regexForWrapper = /<pre([\s\S]*?)><code>([\s\S]*?)<\/code><\/pre>/gm; |
62 | | - const code = regexForWrapper.exec(html); |
63 | | - return code ? code[2] : ""; |
64 | | -} |
0 commit comments