Skip to content

Commit c595834

Browse files
authored
Merge pull request #246 from inertiajs/docs-available-since
Docs: add Available since component
2 parents 65f1175 + 8d0fa0b commit c595834

File tree

8 files changed

+570
-12
lines changed

8 files changed

+570
-12
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import MarkdownIt from 'markdown-it'
2+
3+
export interface AvailableSinceParams {
4+
rails?: string
5+
core?: string
6+
description?: string
7+
}
8+
9+
function parseAvailableSinceParams(info: string): AvailableSinceParams {
10+
const basicMatch = info.trim().match(/^available_since(?:\s+(.*))?$/)
11+
if (!basicMatch) return {}
12+
13+
const allParams = basicMatch[1] || ''
14+
const params: AvailableSinceParams = {}
15+
16+
// Parse out key=value pairs first
17+
const keyValueMatches = [
18+
...allParams.matchAll(/([a-z]+)(?:=("[^"]*"|[^\s"]+))?/g),
19+
]
20+
for (const [, key, value] of keyValueMatches) {
21+
let cleanValue = value ? value.replace(/^"|"$/g, '') : true
22+
23+
if (key === 'rails') params.rails = cleanValue as string
24+
if (key === 'core') params.core = cleanValue as string
25+
if (key === 'description') params.description = cleanValue as string
26+
}
27+
28+
return params
29+
}
30+
31+
export function availableSinceMarkdownPlugin(md: MarkdownIt) {
32+
md.block.ruler.before(
33+
'paragraph',
34+
'available_since_oneliner',
35+
(state, start, end, silent) => {
36+
const line = state.getLines(start, start + 1, 0, false).trim()
37+
38+
const match = line.match(/^@available_since\s+(.+)$/)
39+
if (!match) return false
40+
41+
if (silent) return true
42+
43+
const params = parseAvailableSinceParams(`available_since ${match[1]}`)
44+
const token = state.push('available_since_oneliner', '', 0)
45+
46+
token.content = renderAvailableSince(params, md)
47+
token.map = [start, start + 1]
48+
49+
state.line = start + 1
50+
return true
51+
},
52+
)
53+
54+
// Render the one-liner available_since token
55+
md.renderer.rules.available_since_oneliner = (tokens, idx) => {
56+
return tokens[idx].content + '\n'
57+
}
58+
}
59+
60+
function renderAvailableSince(
61+
params: AvailableSinceParams,
62+
md: MarkdownIt,
63+
): string {
64+
const railsAttr = params.rails
65+
? `rails="${md.utils.escapeHtml(params.rails)}"`
66+
: ''
67+
const coreAttr = params.core
68+
? `core="${md.utils.escapeHtml(params.core)}"`
69+
: ''
70+
const descriptionAttr = params.description
71+
? `description="${md.utils.escapeHtml(params.description)}"`
72+
: ''
73+
74+
return `<AvailableSince ${railsAttr} ${coreAttr} ${descriptionAttr} />`
75+
}

docs/.vitepress/config.mts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { defineConfig } from 'vitepress'
22
import llmstxt from 'vitepress-plugin-llms'
3+
import { availableSinceMarkdownPlugin } from './availableSinceMarkdownPlugin'
34
import { tabsMarkdownPlugin } from './vitepress-plugin-tabs/tabsMarkdownPlugin'
45

56
const title = 'Inertia Rails'
@@ -25,6 +26,7 @@ export default defineConfig({
2526
markdown: {
2627
config(md) {
2728
md.use(tabsMarkdownPlugin)
29+
md.use(availableSinceMarkdownPlugin)
2830
},
2931
},
3032

0 commit comments

Comments
 (0)