Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite custom CSS for builded static pages #1360

Merged
merged 1 commit into from
Feb 12, 2025
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
4 changes: 3 additions & 1 deletion frontend/src/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ const MyDocument: React.FC = () => {
--color-events: ${events};
}
`}</style>
{style !== undefined && <style dangerouslySetInnerHTML={{ __html: style }} />}
{style !== undefined && (
<style className="custo-style-file" dangerouslySetInnerHTML={{ __html: style }} />
)}
</Head>
<body>
<Main />
Expand Down
58 changes: 41 additions & 17 deletions frontend/src/services/rewriteBuildedPages.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,20 @@ import { runtimeConfig } from './getConfig.mjs';
/** @type {ColorsConfig} */
const colors = runtimeConfig.colors;

const colorsAsString = Object.entries(colors).reduce(
(list, [key, value]) => {
if (!value) {
return list;
}
if (typeof value === 'string') {
list.push(`--color-${key}: ${value}`.toLowerCase());
} else if(typeof value === 'object' && !Array.isArray(value)) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
Object.entries(value).forEach(item => {
list.push(`--color-${key}-${item[0]}: ${item[1]}`.toLowerCase());
});
}
const colorsAsString = Object.entries(colors).reduce((list, [key, value]) => {
if (!value) {
return list;
},
[],
);
}
if (typeof value === 'string') {
list.push(`--color-${key}: ${value}`.toLowerCase());
} else if (typeof value === 'object' && !Array.isArray(value)) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
Object.entries(value).forEach(item => {
list.push(`--color-${key}-${item[0]}: ${item[1]}`.toLowerCase());
});
}
return list;
}, []);

const rewriteBuildedPages = () => {
const pages = ['404', '_offline', 'offline'];
Expand All @@ -33,7 +30,7 @@ const rewriteBuildedPages = () => {
// All HTML configuration will not be displayed. Scripts are removed to avoid breaking the page
.replace(new RegExp('<script(.*?)</script>', 'g'), '');

runtimeConfig.header.menu.supportedLanguages.forEach(lang => {
runtimeConfig.header.menu.supportedLanguages.forEach(lang => {
pages.forEach(page => {
if (!fs.existsSync(`./src/.next/server/pages/${lang}/${page}.html`)) {
return;
Expand All @@ -49,6 +46,33 @@ const rewriteBuildedPages = () => {
);
}

// Inject custom Style
if (runtimeConfig.style) {
if (file.includes('<style class="custo-style-file">')) {
// Replace content
file = `${file}`.replace(
// eslint-disable-next-line no-control-regex
new RegExp('<style class="custo-style-file">(.|\n)*?</style>'),
`<style class="custo-style-file">${runtimeConfig.style}</style>`,
);
} else {
// Or add it
file = `${file}`.replace(
'</head>',
`<style class="custo-style-file">${runtimeConfig.style}</style></head>`,
);
}
} else {
if (file.includes('<style class="custo-style-file">')) {
// Remove it
file = `${file}`.replace(
// eslint-disable-next-line no-control-regex
new RegExp('<style class="custo-style-file">(.|\n)*?</style>'),
'',
);
}
}

// Replace colors
colorsAsString.forEach(item => {
const [key, value] = item.split(': ');
Expand Down
Loading