Skip to content

Commit

Permalink
refactor: plugins & config
Browse files Browse the repository at this point in the history
  • Loading branch information
WRXinYue committed Sep 20, 2024
1 parent 142e97a commit 43265dc
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 87 deletions.
55 changes: 55 additions & 0 deletions theme/node/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import type { ThemeConfig } from '../types'

/**
* Default Config
*/
export const defaultThemeConfig: ThemeConfig = {
valaxyDarkOptions: {
circleTransition: true,
},

colors: {
primary: '#0078E7',
},

footer: {
since: 2022,
icon: {
name: 'i-ri-cloud-line',
animated: true,
color: 'var(--va-c-primary)',
url: 'https://sponsors.yunyoujun.cn',
title: 'Sponsor YunYouJun',
},

powered: true,

beian: {
enable: false,
icp: '',
},
},

nav: [],

subNav: [],
}

/**
* generateSafelist by config
* @param themeConfig
*/
export function generateSafelist(themeConfig: ThemeConfig) {
const safelist: string[] = ['i-ri-menu-2-fill']

const footerIcon = themeConfig.footer?.icon?.name
if (footerIcon)
safelist.push(footerIcon)

themeConfig.nav?.forEach((navItem) => {
if (navItem.icon)
safelist.push(navItem.icon)
})

return safelist
}
83 changes: 2 additions & 81 deletions theme/node/index.ts
Original file line number Diff line number Diff line change
@@ -1,81 +1,2 @@
import type { Plugin } from 'vite'
import type { ResolvedValaxyOptions } from 'valaxy'
import type { ThemeConfig } from '../types'

/**
* Default Config
*/
export const defaultThemeConfig: ThemeConfig = {
valaxyDarkOptions: {
circleTransition: true,
},

colors: {
primary: '#0078E7',
},

footer: {
since: 2022,
icon: {
name: 'i-ri-cloud-line',
animated: true,
color: 'var(--va-c-primary)',
url: 'https://sponsors.yunyoujun.cn',
title: 'Sponsor YunYouJun',
},

powered: true,

beian: {
enable: false,
icp: '',
},
},

nav: [],

subNav: [],
}

// write a vite plugin
// https://vitejs.dev/guide/api-plugin.html
export function themePlugin(options: ResolvedValaxyOptions<ThemeConfig>): Plugin {
const themeConfig = options.config.themeConfig || {}

return {
name: 'valaxy-theme-starter',

config() {
return {
css: {
preprocessorOptions: {
scss: {
additionalData: `$c-primary: ${themeConfig.colors?.primary || '#0078E7'} !default;`,
},
},
},

valaxy: {},
}
},
}
}

/**
* generateSafelist by config
* @param themeConfig
*/
export function generateSafelist(themeConfig: ThemeConfig) {
const safelist: string[] = ['i-ri-menu-2-fill']

const footerIcon = themeConfig.footer?.icon?.name
if (footerIcon)
safelist.push(footerIcon)

themeConfig.nav?.forEach((navItem) => {
if (navItem.icon)
safelist.push(navItem.icon)
})

return safelist
}
export * from './config'
export * from './plugins'
32 changes: 28 additions & 4 deletions theme/vite-plugin/valaxy-blog-plugin.ts → theme/node/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
import { promises as fs } from 'node:fs'
import path, { basename } from 'node:path'
import type { Plugin } from 'vite'
import type { ResolvedValaxyOptions } from 'valaxy'
import type { ThemeConfig } from '../types'

// write a vite plugin
// https://vitejs.dev/guide/api-plugin.html
export function themePlugin(options: ResolvedValaxyOptions<ThemeConfig>): Plugin {
const themeConfig = options.config.themeConfig || {}

return {
name: 'valaxy-theme-starter',

config() {
return {
css: {
preprocessorOptions: {
scss: {
additionalData: `$c-primary: ${themeConfig.colors?.primary || '#0078E7'} !default;`,
},
},
},

valaxy: {},
}
},
}
}

/**
* @see https://github.com/vitejs/vite/issues/14102
Expand All @@ -10,15 +36,15 @@ import type { Plugin } from 'vite'
* to access user files in themes.
* Reference https://github.com/vitejs/vite/issues/2351
*/
function ValaxyBlogPlugin({ prefix = '@valaxy-blog/' }: { prefix?: string } = {}): Plugin {
export function ValaxyBlogPlugin({ prefix = '@valaxy-blog/' }: { prefix?: string } = {}): Plugin {
const sourceFromId = (id: string) => id.slice(prefix.length)
return {
name: `ValaxyBlogPlugin`,
enforce: 'pre',

async resolveId(id: string): Promise<any> {
if (id.startsWith(prefix)) {
return path.resolve(sourceFromId(id))
return path.resolve(`./${sourceFromId(id)}`)
}
},

Expand All @@ -41,5 +67,3 @@ function ValaxyBlogPlugin({ prefix = '@valaxy-blog/' }: { prefix?: string } = {}
},
}
}

export default ValaxyBlogPlugin
8 changes: 6 additions & 2 deletions theme/valaxy.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import Shiki from '@shikijs/markdown-it'
import { rendererRich, transformerTwoslash } from '@shikijs/twoslash'
// import TOC from 'markdown-it-table-of-contents'
import type { ThemeConfig } from './types'
import { defaultThemeConfig, generateSafelist, themePlugin } from './node'
import { ValaxyBlogPlugin, defaultThemeConfig, generateSafelist, themePlugin } from './node'
import { slugify } from './scripts/slugify'
import ValaxyBlogPlugin from './vite-plugin/valaxy-blog-plugin'

const promises: Promise<any>[] = []

Expand Down Expand Up @@ -50,6 +49,11 @@ export default defineTheme<ThemeConfig>((options) => {

ValaxyBlogPlugin(),
],
resolve: {
alias: {

},
},
},
markdown: {
// Waiting for Valaxy support
Expand Down

0 comments on commit 43265dc

Please sign in to comment.