Skip to content

Commit 8bde330

Browse files
authored
Merge pull request #379 from devforth/ScriptInHtmlHead
feat: add possibility to add inner code in html head elements
2 parents e488da8 + a5901de commit 8bde330

File tree

5 files changed

+27
-3
lines changed

5 files changed

+27
-3
lines changed

adminforth/documentation/docs/tutorial/03-Customization/08-pageInjections.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,4 +554,24 @@ new AdminForth({
554554
})
555555
```
556556
557-
If you hide the logo with `showBrandLogoInSidebar: false`, components injected via `sidebarTop` will take the whole line width.
557+
If you hide the logo with `showBrandLogoInSidebar: false`, components injected via `sidebarTop` will take the whole line width.
558+
559+
## Custom scripts in head
560+
561+
If you want to inject tags in your html head:
562+
563+
```ts title='./index.ts'
564+
565+
customization: {
566+
...
567+
customHeadItems: [
568+
{
569+
tagName: 'script',
570+
attributes: { async: 'true', defer: 'true' },
571+
innerCode: "console.log('Hello from HTML head')"
572+
}
573+
],
574+
...
575+
}
576+
577+
```

adminforth/modules/codeInjector.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,14 +570,14 @@ class CodeInjector implements ICodeInjector {
570570
// inject heads to index.html
571571
const headItems = this.adminforth.config.customization?.customHeadItems;
572572
if(headItems){
573-
const renderedHead = headItems.map(({ tagName, attributes }) => {
573+
const renderedHead = headItems.map(({ tagName, attributes, innerCode }) => {
574574
const attrs = Object.entries(attributes)
575575
.map(([key, value]) => `${key}="${value}"`)
576576
.join(' ');
577577
const isVoid = ['base', 'link', 'meta'].includes(tagName);
578578
return isVoid
579579
? `<${tagName} ${attrs}>`
580-
: `<${tagName} ${attrs}></${tagName}>`;
580+
: `<${tagName} ${attrs}> ${innerCode} </${tagName}>`;
581581
}).join('\n ');
582582

583583
indexHtmlContent = indexHtmlContent.replace(" <!-- /* IMPORTANT:ADMINFORTH HEAD */ -->", `${renderedHead}` );

adminforth/spa/src/spa_types/core.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export type CoreConfig = {
4949
customHeadItems?: {
5050
tagName: string;
5151
attributes: { [key: string]: string | boolean };
52+
innerCode?: string;
5253
}[],
5354
}
5455

adminforth/types/Back.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,7 @@ interface AdminForthInputConfigCustomization {
811811
customHeadItems?: {
812812
tagName: string;
813813
attributes: Record<string, string | boolean>;
814+
innerCode?: string;
814815
}[];
815816

816817
}
@@ -1140,6 +1141,7 @@ export interface AdminForthConfigCustomization extends Omit<AdminForthInputConfi
11401141
customHeadItems?: {
11411142
tagName: string;
11421143
attributes: Record<string, string | boolean>;
1144+
innerCode?: string;
11431145
}[];
11441146

11451147
}

adminforth/types/Common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,7 @@ export interface AdminForthConfigForFrontend {
11081108
customHeadItems?: {
11091109
tagName: string;
11101110
attributes: Record<string, string | boolean>;
1111+
innerCode?: string;
11111112
}[],
11121113
settingPages?:{
11131114
icon?: string,

0 commit comments

Comments
 (0)