Skip to content

Commit 926a211

Browse files
committed
build: update API generator
1 parent 6f0e81e commit 926a211

File tree

3 files changed

+63
-14
lines changed

3 files changed

+63
-14
lines changed

packages/docs/build/api.mjs

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,21 @@ import { writeFile, mkdir } from 'node:fs/promises'
77
import path from 'node:path'
88
import { fileURLToPath } from 'node:url'
99
import { parse } from 'react-docgen-typescript'
10+
import showdown from 'showdown'
1011

1112
/**
1213
* Derive __dirname in ESM
1314
*/
1415
const __filename = fileURLToPath(import.meta.url)
1516
const __dirname = path.dirname(__filename)
17+
const converter = new showdown.Converter({ simpleLineBreaks: true })
1618

1719
/**
1820
* Glob patterns to locate .tsx files for documentation.
1921
* Adjust these patterns based on your project structure.
2022
*/
2123
const GLOB_PATTERNS = [
24+
// '**/src/components/date-picker/*.tsx',
2225
'**/src/**/*.tsx',
2326
'../node_modules/@coreui/icons-react/src/**/*.tsx',
2427
'../node_modules/@coreui/react-chartjs/src/**/*.tsx',
@@ -44,26 +47,39 @@ const EXCLUDED_FILES = [] // Currently unused, but can be utilized if needed
4447
* Options for react-docgen-typescript parser.
4548
*/
4649
const DOCGEN_OPTIONS = {
47-
savePropValueAsString: true,
4850
shouldIncludePropTagMap: true,
4951
}
5052

5153
/**
5254
* List of pro components that require special handling.
5355
*/
5456
const PRO_COMPONENTS = [
57+
'CCalendar',
5558
'CDatePicker',
5659
'CDateRangePicker',
5760
'CFormMask',
5861
'CLoadingButton',
5962
'CMultiSelect',
6063
'CRating',
64+
'CRangeSlider',
65+
'CRating',
6166
'CSmartPagination',
6267
'CSmartTable',
6368
'CTimePicker',
6469
'CVirtualScroller',
6570
]
6671

72+
const TEXT_REPLACEMENTS = {
73+
CDatePicker: {
74+
description: [{ 'React Calendar': 'React Date Picker' }],
75+
example: [{ CCalendar: 'CDatePicker' }],
76+
},
77+
CDateRangePicker: {
78+
description: [{ 'React Calendar': 'React Date Range Picker' }],
79+
example: [{ CCalendar: 'CDateRangePicker' }],
80+
},
81+
}
82+
6783
/**
6884
* Escapes special characters in text to prevent Markdown rendering issues.
6985
*
@@ -72,13 +88,10 @@ const PRO_COMPONENTS = [
7288
*/
7389
function escapeMarkdown(text) {
7490
if (typeof text !== 'string') return text
75-
return (
76-
text
77-
.replaceAll(/(<)/g, String.raw`\$1`)
78-
// .replaceAll(/<C(.*)\/>/g, '`<C$1/>`')
79-
.replaceAll('\n', '<br/>')
80-
.replaceAll(/`([^`]+)`/g, '<code>{`$1`}</code>')
81-
)
91+
return text
92+
.replaceAll(/(<)/g, String.raw`\$1`)
93+
.replaceAll('\n', '<br/>')
94+
.replaceAll(/`([^`]+)`/g, '<code>{`$1`}</code>')
8295
}
8396

8497
/**
@@ -110,6 +123,10 @@ function getRelativeFilename(file) {
110123
* @throws {Error} Throws an error if there are unmatched braces or parentheses in the input.
111124
*/
112125
function splitOutsideBracesAndParentheses(input) {
126+
if (input.endsWith('...')) {
127+
return [input]
128+
}
129+
113130
const parts = []
114131
let currentPart = ''
115132
let braceDepth = 0 // Tracks depth of curly braces {}
@@ -172,6 +189,23 @@ function splitOutsideBracesAndParentheses(input) {
172189
return parts
173190
}
174191

192+
function replaceText(componenName, keyName, text) {
193+
const keyNames = Object.keys(TEXT_REPLACEMENTS)
194+
195+
if (keyNames.includes(componenName)) {
196+
const replacements = TEXT_REPLACEMENTS[componenName][keyName]
197+
for (const replacement of replacements) {
198+
for (const [key, value] of Object.entries(replacement)) {
199+
if (text && key && value) {
200+
return text.replaceAll(key, value)
201+
}
202+
}
203+
}
204+
} else {
205+
return text
206+
}
207+
}
208+
175209
/**
176210
* Creates an MDX file with the component's API documentation.
177211
*
@@ -190,10 +224,10 @@ async function createMdx(file, component) {
190224
let content = `\n\`\`\`jsx\n`
191225
const importPathParts = relativeFilename.split('/')
192226
if (importPathParts.length > 1) {
193-
content += `import { ${component.displayName} } from '@coreui/${importPathParts[1]}'\n`
227+
content += `import { ${component.displayName} } from '@coreui/${importPathParts[0]}'\n`
194228
}
195229
content += `// or\n`
196-
content += `import ${component.displayName} from '@coreui${relativeFilename.replace('.tsx', '')}'\n`
230+
content += `import ${component.displayName} from '@coreui/${relativeFilename.replace('.tsx', '')}'\n`
197231
content += `\`\`\`\n\n`
198232

199233
const sortedProps = Object.entries(component.props).sort(([a], [b]) => a.localeCompare(b))
@@ -240,14 +274,19 @@ async function createMdx(file, component) {
240274
const deprecated = propInfo.tags?.deprecated
241275
? `<span className="badge bg-success">Deprecated ${propInfo.tags.since}</span>`
242276
: ''
243-
const description = propInfo.description || '-'
277+
const description = propInfo.description
278+
? replaceText(component.displayName, 'description', propInfo.description)
279+
: '-'
244280

245281
const type = propInfo.type
246282
? propInfo.type.name.includes('ReactElement')
247283
? 'ReactElement'
248284
: propInfo.type.name
249285
: ''
250286
const defaultValue = propInfo.defaultValue ? `\`${propInfo.defaultValue.value}\`` : `undefined`
287+
const example = propInfo.tags?.example
288+
? replaceText(component.displayName, 'example', propInfo.tags?.example)
289+
: false
251290

252291
// Format types as inline code
253292
const types = splitOutsideBracesAndParentheses(type)
@@ -263,7 +302,16 @@ async function createMdx(file, component) {
263302
content += ` <td>${escapeMarkdown(types)}</td>\n`
264303
content += ` </tr>\n`
265304
content += ` <tr>\n`
266-
content += ` <td colSpan="3">${escapeMarkdown(description)}${propInfo.tags?.example ? `<br /><JSXDocs code={\`${propInfo.tags.example}\`} />` : ''}</td>\n`
305+
content += ` <td colSpan="3">\n`
306+
content += ` ${converter
307+
.makeHtml(description)
308+
.replaceAll(/<code>(.*?)<\/code>/g, '<code>{`$1`}</code>')}\n`
309+
310+
if (example) {
311+
content += ` <JSXDocs code={\`${example.trim()}\`} />\n`
312+
}
313+
314+
content += ` </td>\n`
267315
content += ` </tr>\n`
268316

269317
if (isLast) {

packages/docs/gatsby-node.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const createPages = async ({
3434
}) => {
3535
const result = await graphql(`
3636
query {
37-
allMdx {
37+
allMdx(filter: { fields: { slug: { regex: "/^(?!/api/).*/" } } }) {
3838
nodes {
3939
id
4040
fields {

packages/docs/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@
6060
"react-imask": "^7.6.1",
6161
"react-markdown": "^9.0.1",
6262
"rimraf": "^6.0.1",
63-
"sass": "^1.80.4"
63+
"sass": "^1.80.4",
64+
"showdown": "^2.1.0"
6465
},
6566
"devDependencies": {
6667
"npm-run-all": "^4.1.5"

0 commit comments

Comments
 (0)