Skip to content

Commit d9da154

Browse files
author
yaroslav8765
committed
feat: add ability to add cutoms items to head
1 parent f325691 commit d9da154

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

adminforth/modules/codeInjector.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,8 +545,21 @@ class CodeInjector implements ICodeInjector {
545545
||
546546
`/assets/favicon.png`
547547
);
548-
await fs.promises.writeFile(indexHtmlPath, indexHtmlContent);
549548

549+
// inject heads to index.html
550+
const headItems = this.adminforth.config.customization!.customHeadItems;
551+
const renderedHead = headItems.map(({ tagName, attributes }) => {
552+
const attrs = Object.entries(attributes)
553+
.map(([k, v]) => `${k}="${v}"`)
554+
.join(' ');
555+
const isVoid = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'source', 'track', 'wbr'].includes(tagName);
556+
return isVoid
557+
? `<${tagName} ${attrs}>`
558+
: `<${tagName} ${attrs}></${tagName}>`;
559+
}).join('\n ');
560+
561+
indexHtmlContent = indexHtmlContent.replace("/* IMPORTANT:ADMINFORTH HEAD */", `${renderedHead}` );
562+
await fs.promises.writeFile(indexHtmlPath, indexHtmlContent);
550563

551564
/* generate custom routes */
552565
let homepageMenuItem: AdminForthConfigMenuItem = findHomePage(this.adminforth.config.menu);

adminforth/spa/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
document.documentElement.classList.remove('dark')
1515
}
1616
</script> -->
17-
17+
/* IMPORTANT:ADMINFORTH HEAD */
1818
</head>
1919
<body class="min-h-screen flex flex-col">
2020
<div id="app" class="grow bg-lightHtml dark:bg-darkHtml w-full"></div>

adminforth/types/Back.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,12 @@ interface AdminForthInputConfigCustomization {
764764
sidebar?: AdminForthComponentDeclaration | Array<AdminForthComponentDeclaration>,
765765
everyPageBottom?: AdminForthComponentDeclaration | Array<AdminForthComponentDeclaration>,
766766
}
767+
768+
customHeadItems?: {
769+
tagName: string;
770+
attributes: Record<string, string | boolean>;
771+
}[];
772+
767773
}
768774

769775
export interface AdminForthActionInput {
@@ -1069,6 +1075,12 @@ export interface AdminForthConfigCustomization extends Omit<AdminForthInputConfi
10691075
sidebar: Array<AdminForthComponentDeclarationFull>,
10701076
everyPageBottom: Array<AdminForthComponentDeclarationFull>,
10711077
},
1078+
1079+
customHeadItems?: {
1080+
tagName: string;
1081+
attributes: Record<string, string | boolean>;
1082+
}[];
1083+
10721084
}
10731085

10741086
export interface AdminForthConfig extends Omit<AdminForthInputConfig, 'customization' | 'resources'> {

0 commit comments

Comments
 (0)