Skip to content

Commit 4e653ef

Browse files
authored
Update support for Angular component highlighting (#319)
* Update support for Angular component highlighting * Remove the fileExt for angularHighligher as it's not used * Remove most of angular highlighter as it's not needed
1 parent 14bf421 commit 4e653ef

4 files changed

Lines changed: 4 additions & 70 deletions

File tree

build/lib/angularHighlighter.d.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
11
export function mustUseAngularHighlighter(fileContent: string): boolean;
2-
3-
export function highlightAngularComponent(
4-
fileContent: string,
5-
fileExt: string,
6-
): Promise<string>;

build/lib/angularHighlighter.ts

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

build/lib/generateContent.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ import path from "node:path";
44
import { frameworks } from "../../frameworks.ts";
55
import playgroundUrlByFramework from "./playgroundUrlByFramework.ts";
66
import prettier from "prettier";
7-
import {
8-
highlightAngularComponent,
9-
mustUseAngularHighlighter,
10-
} from "./angularHighlighter.ts";
7+
import { mustUseAngularHighlighter } from "./angularHighlighter.ts";
118
import {
129
codeToHighlightCodeHtml,
1310
markdownToHighlightedHtml,
@@ -210,7 +207,7 @@ export default async function generateContent(
210207
frameworkSnippet.markdownFiles.push(file);
211208
} else {
212209
file.contentHtml = mustUseAngularHighlighter(content)
213-
? await highlightAngularComponent(content, ext)
210+
? await codeToHighlightCodeHtml(content, "angular-ts")
214211
: await codeToHighlightCodeHtml(content, ext);
215212

216213
frameworkSnippet.files.push(file);

build/lib/highlighter.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ async function getHighlighter(): Promise<
3232
"jsx",
3333
"vue",
3434
"marko",
35+
"angular-ts",
36+
"angular-html",
3537
],
3638
langAlias: {
3739
tsrx: "jsx",

0 commit comments

Comments
 (0)