Skip to content

Commit 9d2419c

Browse files
committed
feat: advanced.functionCodeReplace to modified function code while using funciton type values of config
1 parent 6037eb5 commit 9d2419c

File tree

5 files changed

+83
-2
lines changed

5 files changed

+83
-2
lines changed

docs/astro.config.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default defineConfig({
1616
post: {
1717
pathStyle: 'id',
1818
entryProcessors: [
19-
[processorUpdateModifiedTime, { mode: 'fs' }],
19+
[processorUpdateModifiedTime, { mode: 'git' }],
2020
],
2121
},
2222
collections: {
@@ -66,6 +66,15 @@ export default defineConfig({
6666
{ label: 'Friday', link: 'https://github.com/byronogis/astro-friday/', desc: 'A content-focused Astro integration with tag and series support.', icon: 'i-lucide:lightbulb', category: 'Integration' },
6767
{ label: 'Friday', link: 'https://github.com/byronogis/astro-friday/', desc: 'A content-focused Astro integration with tag and series support.', icon: 'i-lucide:lightbulb', category: 'Integration' },
6868
],
69+
advanced: {
70+
functionCodeReplace: [
71+
// processorUpdateModifiedTime compatibility
72+
{
73+
api: 'replaceAll',
74+
parameters: ['__vite_ssr_dynamic_import__("node:', 'import("node:'],
75+
},
76+
],
77+
},
6978
}),
7079
],
7180
server: {

packages/astro-friday/src/config.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ export function getDefaultConfig(userConfig: Config, astroConfig: AstroConfig):
177177
].flat(),
178178
},
179179
projects: [],
180+
advanced: {
181+
functionCodeReplace: [],
182+
},
180183
}
181184
}
182185

@@ -569,6 +572,46 @@ export interface Config {
569572
* @default []
570573
*/
571574
projects?: ProjectItem[]
575+
/**
576+
* Advanced configuration options for power using.
577+
*/
578+
advanced?: {
579+
/**
580+
* Function code replacement configuration for advanced using.
581+
*
582+
* When passing functions as configuration values, you might want to replace
583+
* certain code snippets.
584+
*
585+
* Like we can solve the `__vite_ssr_dynamic_import__ is not defined` issue
586+
* by replacing `__vite_ssr_dynamic_import__("node:fs")` with
587+
* `import("node:fs")` when building.
588+
*
589+
* @example
590+
* ```ts
591+
* advanced: {
592+
* functionCodeReplace: [
593+
* {
594+
* api: 'replaceAll',
595+
* parameters: ['__vite_ssr_dynamic_import__("node:', 'import("node:'],
596+
* },
597+
* ],
598+
* }
599+
* ```
600+
*/
601+
functionCodeReplace?: {
602+
/**
603+
* The string method to replace function code snippets
604+
*
605+
* @default 'replaceAll'
606+
*/
607+
api?: 'replace' | 'replaceAll'
608+
/**
609+
* Just allow string parameters due to serialization limitation, will
610+
* pass to the string method after spreading.
611+
*/
612+
parameters: [string, string]
613+
}[]
614+
}
572615
}
573616

574617
export type ResolvedConfig = SetRequiredDeep<
@@ -616,6 +659,8 @@ export type ResolvedConfig = SetRequiredDeep<
616659
| 'integrations.robotsTxt'
617660
| 'integrations.mdx'
618661
| 'projects'
662+
| 'advanced'
663+
| 'advanced.functionCodeReplace'
619664
> & {
620665
/**
621666
* The full base path, including Astro's base.

packages/astro-friday/src/integration.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export function integration(userConfig: Config = {}): AstroIntegration {
5151
addWatchFile(path.resolve(_dirname, './config.ts'))
5252
addWatchFile(path.resolve(_dirname, './integration.ts'))
5353
addWatchFile(path.resolve(_dirname, './integrations/unocss/uno.config.ts'))
54+
addWatchFile(path.resolve(_dirname, './plugins/config.ts'))
5455
}
5556

5657
updateConfig({

packages/astro-friday/src/plugins/config.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,16 @@ export function vitePluginAstroFridayConfig(resolvedConfig: ResolvedConfig) {
4343

4444
if (id === resolvedFunctionsModuleId) {
4545
const functionDeclarations = Array.from(functionMap.entries())
46-
.map(([funcName, funcCode]) => `const ${funcName} = ${funcCode};`)
46+
.map(([funcName, funcCode]) => {
47+
const processedCode = resolvedConfig.advanced.functionCodeReplace.reduce((code, replaceRule) => {
48+
const {
49+
api = 'replaceAll',
50+
parameters,
51+
} = replaceRule
52+
return code[api](...parameters)
53+
}, funcCode)
54+
return `const ${funcName} = ${processedCode};`
55+
})
4756
.join('\n')
4857

4958
const exportNames = Array.from(functionMap.keys()).join(', ')

packages/astro-friday/src/utils/processor/processorUpdateModifiedTime.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
import type { Processor } from './index'
22

3+
/**
4+
* With this processor, you can automatically update the `modified` frontmatter
5+
* key of your markdown files to reflect their last modified time.
6+
*
7+
* NOTE:
8+
* Using this processor should with the below config of `advanced.functionCodeReplace`:
9+
* ```ts
10+
* advanced: {
11+
* functionCodeReplace: [
12+
* {
13+
* api: 'replaceAll',
14+
* arguments: ['__vite_ssr_dynamic_import__("node:', 'import("node:'],
15+
* },
16+
* ],
17+
* }
18+
* ```
19+
*/
320
export const processorUpdateModifiedTime: Processor<[RemarkModifiedTimeOptions]> = function (options: RemarkModifiedTimeOptions = {}) {
421
const {
522
mode = 'git',

0 commit comments

Comments
 (0)