Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions build/lib/angularHighlighter.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
export function mustUseAngularHighlighter(fileContent: string): boolean;

export function highlightAngularComponent(
fileContent: string,
fileExt: string,
): Promise<string>;
60 changes: 0 additions & 60 deletions build/lib/angularHighlighter.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,5 @@
import { codeToHighlightCodeHtml } from "./highlighter.ts";

export function mustUseAngularHighlighter(fileContent: string): boolean {
return (
fileContent.includes("@angular/core") && fileContent.includes("template")
);
}

export async function highlightAngularComponent(
fileContent: string,
fileExt: string,
): Promise<string> {
const templateCode = getAngularTemplateCode(fileContent);

let codeHighlighted;
if (templateCode) {
const componentWithEmptyTemplate =
removeAngularTemplateContent(fileContent);
const templateCodeHighlighted = await codeToHighlightCodeHtml(
templateCode,
"html",
);

const componentWithoutTemplateHighlighted = await codeToHighlightCodeHtml(
componentWithEmptyTemplate,
fileExt,
);

codeHighlighted = componentWithoutTemplateHighlighted.replace(
"template",
"template: `" + removeCodeWrapper(templateCodeHighlighted) + "`,",
);
} else {
codeHighlighted = await codeToHighlightCodeHtml(fileContent, fileExt);
}

return codeHighlighted;
}

function getAngularTemplateCode(fileContent: string): string {
// regex to grab what is inside angular component template inside backticks
const regex = /template:\s*`([\s\S]*?)`/gm;

// grab the template string
const template = regex.exec(fileContent);

if (template) return template[1];

return "";
}

function removeAngularTemplateContent(fileContent: string): string {
const componentWithoutContentInsideTemplate = fileContent.replace(
/template:\s*`([\s\S]*?)([^*])`,?/gm,
"template",
);

return componentWithoutContentInsideTemplate;
}

function removeCodeWrapper(html: string): string {
const regexForWrapper = /<pre([\s\S]*?)><code>([\s\S]*?)<\/code><\/pre>/gm;
const code = regexForWrapper.exec(html);
return code ? code[2] : "";
}
7 changes: 2 additions & 5 deletions build/lib/generateContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import path from "node:path";
import { frameworks } from "../../frameworks.ts";
import playgroundUrlByFramework from "./playgroundUrlByFramework.ts";
import prettier from "prettier";
import {
highlightAngularComponent,
mustUseAngularHighlighter,
} from "./angularHighlighter.ts";
import { mustUseAngularHighlighter } from "./angularHighlighter.ts";
import {
codeToHighlightCodeHtml,
markdownToHighlightedHtml,
Expand Down Expand Up @@ -210,7 +207,7 @@ export default async function generateContent(
frameworkSnippet.markdownFiles.push(file);
} else {
file.contentHtml = mustUseAngularHighlighter(content)
? await highlightAngularComponent(content, ext)
? await codeToHighlightCodeHtml(content, "angular-ts")
: await codeToHighlightCodeHtml(content, ext);

frameworkSnippet.files.push(file);
Expand Down
2 changes: 2 additions & 0 deletions build/lib/highlighter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ async function getHighlighter(): Promise<
"jsx",
"vue",
"marko",
"angular-ts",
"angular-html",
],
langAlias: {
tsrx: "jsx",
Expand Down
Loading