Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions docs/.vitepress/availableSinceMarkdownPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import MarkdownIt from 'markdown-it'

export interface AvailableSinceParams {
rails?: string
core?: string
description?: string
}

function parseAvailableSinceParams(info: string): AvailableSinceParams {
const basicMatch = info.trim().match(/^available_since(?:\s+(.*))?$/)
if (!basicMatch) return {}

const allParams = basicMatch[1] || ''
const params: AvailableSinceParams = {}

// Parse out key=value pairs first
const keyValueMatches = [
...allParams.matchAll(/([a-z]+)(?:=("[^"]*"|[^\s"]+))?/g),
]
for (const [, key, value] of keyValueMatches) {
let cleanValue = value ? value.replace(/^"|"$/g, '') : true

if (key === 'rails') params.rails = cleanValue as string
if (key === 'core') params.core = cleanValue as string
if (key === 'description') params.description = cleanValue as string
}

return params
}

export function availableSinceMarkdownPlugin(md: MarkdownIt) {
md.block.ruler.before(
'paragraph',
'available_since_oneliner',
(state, start, end, silent) => {
const line = state.getLines(start, start + 1, 0, false).trim()

const match = line.match(/^@available_since\s+(.+)$/)
if (!match) return false

if (silent) return true

const params = parseAvailableSinceParams(`available_since ${match[1]}`)
const token = state.push('available_since_oneliner', '', 0)

token.content = renderAvailableSince(params, md)
token.map = [start, start + 1]

state.line = start + 1
return true
},
)

// Render the one-liner available_since token
md.renderer.rules.available_since_oneliner = (tokens, idx) => {
return tokens[idx].content + '\n'
}
}

function renderAvailableSince(
params: AvailableSinceParams,
md: MarkdownIt,
): string {
const railsAttr = params.rails
? `rails="${md.utils.escapeHtml(params.rails)}"`
: ''
const coreAttr = params.core
? `core="${md.utils.escapeHtml(params.core)}"`
: ''
const descriptionAttr = params.description
? `description="${md.utils.escapeHtml(params.description)}"`
: ''

return `<AvailableSince ${railsAttr} ${coreAttr} ${descriptionAttr} />`
}
2 changes: 2 additions & 0 deletions docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { defineConfig } from 'vitepress'
import llmstxt from 'vitepress-plugin-llms'
import { availableSinceMarkdownPlugin } from './availableSinceMarkdownPlugin'
import { tabsMarkdownPlugin } from './vitepress-plugin-tabs/tabsMarkdownPlugin'

const title = 'Inertia Rails'
Expand All @@ -25,6 +26,7 @@ export default defineConfig({
markdown: {
config(md) {
md.use(tabsMarkdownPlugin)
md.use(availableSinceMarkdownPlugin)
},
},

Expand Down
Loading