Skip to content

Commit 4d4489a

Browse files
committed
Refactor version-replacer to substitution replacer and expand with more options
1 parent e44c02a commit 4d4489a

4 files changed

Lines changed: 105 additions & 37 deletions

File tree

.vitepress/config.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import { defineConfig } from 'vitepress'
2-
import { versionReplacer } from './plugins/version-replacer.js'
2+
import { substitutionsReplacer } from './plugins/substitutions-replacer.js'
33
import { deepMerge, loadConfigOverrides, applyBaseToHeadTags } from './utils.js'
44

55
const defaultConfig = {
66
srcDir: 'docs',
77
title: 'CakePHP',
88
description: 'CakePHP Documentation - The rapid development PHP framework',
99
ignoreDeadLinks: true,
10-
phpVersions: {
11-
phpversion: '8.4',
12-
minphpversion: '8.1'
10+
substitutions: {
11+
'|phpversion|': { value: '8.4', format: 'bold' },
12+
'|minphpversion|': { value: '8.1', format: 'italic' },
13+
// Add more substitutions here as needed
14+
// '|cakeversion|': { value: '5.1', format: 'bold' },
15+
// '|projectname|': 'CakePHP', // Simple string without formatting
1316
},
1417
head: [
1518
['link', { rel: 'icon', type: 'image/png', href: '/favicon/favicon-96x96.png', sizes: '96x96' }],
@@ -59,7 +62,7 @@ const mergedConfig = deepMerge(defaultConfig, overrides)
5962

6063
// Configure markdown plugins after mergedConfig is available
6164
mergedConfig.markdown.config = (md) => {
62-
md.use(versionReplacer, mergedConfig.phpVersions || {})
65+
md.use(substitutionsReplacer, { substitutions: mergedConfig.substitutions || {} })
6366
}
6467

6568
// Apply base path to head tags if base is specified
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Markdown-it plugin for replacing text placeholders
3+
*
4+
* Replaces custom placeholders like |phpversion| with configured values.
5+
* Supports optional formatting (bold, italic, code, or none).
6+
*/
7+
8+
/**
9+
* Format a value based on the specified format type
10+
* @param {string} value - The value to format
11+
* @param {string} format - Format type: 'bold', 'italic', 'code', or 'none'
12+
* @returns {string} Formatted value
13+
*/
14+
function formatValue(value, format = 'none') {
15+
switch (format) {
16+
case 'bold':
17+
return `**${value}**`
18+
case 'italic':
19+
return `*${value}*`
20+
case 'code':
21+
return `\`${value}\``
22+
default:
23+
return value
24+
}
25+
}
26+
27+
/**
28+
* Create a text substitution plugin
29+
* @param {Object} md - markdown-it instance
30+
* @param {Object} options - plugin options
31+
* @param {Object} options.substitutions - Object with placeholder keys and values
32+
* @param {string} options.phpversion - Legacy: Current PHP version (deprecated, use substitutions)
33+
* @param {string} options.minphpversion - Legacy: Minimum PHP version (deprecated, use substitutions)
34+
* @returns {void}
35+
*/
36+
export function substitutionsReplacer(md, options = {}) {
37+
let substitutions = {}
38+
39+
// Support new substitutions format
40+
if (options.substitutions) {
41+
substitutions = options.substitutions
42+
}
43+
// Backward compatibility: convert old phpVersions format
44+
else if (options.phpversion || options.minphpversion) {
45+
substitutions = {
46+
'|phpversion|': { value: options.phpversion || '8.4', format: 'bold' },
47+
'|minphpversion|': { value: options.minphpversion || '8.1', format: 'italic' }
48+
}
49+
}
50+
51+
// Store original render method
52+
const originalRender = md.render.bind(md)
53+
54+
md.render = function(src, env = {}) {
55+
// Process each substitution
56+
for (const [placeholder, config] of Object.entries(substitutions)) {
57+
// Support both simple string values and objects with format options
58+
const value = typeof config === 'string' ? config : config.value
59+
const format = typeof config === 'object' && config.format ? config.format : 'none'
60+
61+
// Create regex to match the placeholder (escape special regex characters)
62+
const escapedPlaceholder = placeholder.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
63+
const regex = new RegExp(escapedPlaceholder, 'g')
64+
65+
// Replace with formatted value
66+
src = src.replace(regex, formatValue(value, format))
67+
}
68+
69+
return originalRender(src, env)
70+
}
71+
}
72+
73+
export default substitutionsReplacer

.vitepress/plugins/version-replacer.js

Lines changed: 0 additions & 32 deletions
This file was deleted.

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,30 @@ export default {
7777

7878
All markdown documentation files should be placed in the `docs/` directory.
7979

80+
### Text Substitutions
81+
82+
You can use placeholders in your markdown files that will be automatically replaced with configured values. This is useful for version numbers or other values that need to be updated across multiple files.
83+
84+
**Configuration** (`config.js`):
85+
86+
```javascript
87+
export default {
88+
substitutions: {
89+
'|phpversion|': { value: '8.4', format: 'bold' },
90+
'|minphpversion|': { value: '8.1', format: 'italic' },
91+
'|myversion|': '1.0.0' // Simple string without formatting
92+
}
93+
}
94+
```
95+
96+
**Usage in Markdown**:
97+
98+
```markdown
99+
This plugin requires PHP |phpversion| or higher (minimum |minphpversion|).
100+
```
101+
102+
**Result**: This plugin requires PHP **8.4** or higher (minimum *8.1*).
103+
80104
## Project Structure
81105

82106
```

0 commit comments

Comments
 (0)