Skip to content

Commit

Permalink
chore: extract label config out of css
Browse files Browse the repository at this point in the history
  • Loading branch information
djdjz7 committed Dec 25, 2024
1 parent 0572b6c commit f81bfef
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 6 deletions.
35 changes: 35 additions & 0 deletions generator/base-css-generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { PluginOption } from 'vite'
import fs from 'fs'
import { SiteConfiguration } from '../src/site'

export default function newsListGenerator(): PluginOption {
const virtualModuleId = 'virtual:base.css'
const resolvedVirtualModuleId = '\0' + virtualModuleId

return {
name: 'base-css-generator',
resolveId(id) {
if (id === virtualModuleId) {
return resolvedVirtualModuleId
}
},
load(id) {
if (id === resolvedVirtualModuleId) {
const base = fs.readFileSync('./src/assets/base.css', 'utf-8')
return base
.replace(
'/* warning text */',
SiteConfiguration.markdown.container.warningLabel ?? 'WARNING',
)
.replace('/* error text */', SiteConfiguration.markdown.container.errorLabel ?? 'ERROR')
.replace('/* info text */', SiteConfiguration.markdown.container.infoLabel ?? 'INFO')
}
},
handleHotUpdate({ server, file }) {
if (file.includes('src/assets/base.css')) {
const thisModule = server.moduleGraph.getModuleById(resolvedVirtualModuleId)
if (thisModule) return [thisModule]
}
},
}
}
8 changes: 7 additions & 1 deletion generator/content-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import matter from 'gray-matter'
import Shiki from '@shikijs/markdown-it'
import MathJax3 from 'markdown-it-mathjax3'
import MarkdownItContainer from 'markdown-it-container'
import { SiteConfiguration } from '../src/site'

const md = mdit
.default({
Expand All @@ -19,7 +20,12 @@ const md = mdit
.use(MarkdownItContainer, 'expander', {
render: (tokens: Token[], idx: number) => {
if (tokens[idx].nesting === 1) {
return `<ExpanderComponent class="expander" :initial-collapsed="true" :extend-toggle-area="true"><template #header><span font-bold text-sm p-y-4>MORE</span></template>\n`
return `
<ExpanderComponent class="expander" :initial-collapsed="true"
:extend-toggle-area="true">
<template #header>
<span font-bold text-sm p-y-4>${SiteConfiguration.markdown.container.expanderLabel ?? 'MORE'}</span>
</template>\n`
} else {
return '</ExpanderComponent>\n'
}
Expand Down
12 changes: 9 additions & 3 deletions src/assets/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ body {
}

:root {
--warning-text: '/* warning text */';
--error-text: '/* error text */';
--info-text: '/* info text */';

/* NOT USED: refer to ../generator/content-generator.ts */
--expander-text: '/* expander text */';
font-family:
'Inter',
-apple-system,
Expand Down Expand Up @@ -121,23 +127,23 @@ img {
}

.warning::before {
content: 'WARNING';
content: var(--warning-text);
}

.error {
--at-apply: 'text-red-800 bg-red-300/20 dark:bg-red-500/20 dark:text-red-100';
}

.error::before {
content: 'ERROR';
content: var(--error-text);
}

.info {
--at-apply: 'text-blue-800 bg-blue-300/20 dark:bg-blue-500/20 dark:text-blue-100';
}

.info::before {
content: 'INFO';
content: var(--info-text);
}

.expander {
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'virtual:uno.css'
import './assets/base.css'
import 'virtual:base.css'

import { createSSRApp } from 'vue'
import App from './App.vue'
Expand Down
21 changes: 21 additions & 0 deletions src/site.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
interface SiteConfiguration {
markdown: {
container: {
warningLabel?: string
errorLabel?: string
infoLabel?: string
expanderLabel?: string
}
}
}

export const SiteConfiguration: SiteConfiguration = {
markdown: {
container: {
warningLabel: '警告',
errorLabel: '错误',
infoLabel: '信息',
expanderLabel: '更多',
},
},
}
3 changes: 2 additions & 1 deletion tsconfig.node.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"nightwatch.conf.*",
"playwright.config.*",
"generator/*",
"src/data/*.ts"
"src/data/*.ts",
"src/site.ts"
],
"compilerOptions": {
"composite": true,
Expand Down
2 changes: 2 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import UnoCSS from 'unocss/vite'
import ActivityListGenerator from './generator/activity-list-generator'
import NewsListGenerator from './generator/news-list-generator'
import MarkdownContentGenerator from './generator/content-generator'
import BaseCssGenerator from './generator/base-css-generator'

// https://vite.dev/config/
export default defineConfig({
Expand All @@ -24,6 +25,7 @@ export default defineConfig({
UnoCSS(),
ActivityListGenerator(),
NewsListGenerator(),
BaseCssGenerator(),
],
resolve: {
alias: {
Expand Down

0 comments on commit f81bfef

Please sign in to comment.