Skip to content

Instantly share code, notes, and snippets.

@aparajita
Created June 29, 2022 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aparajita/9e48f40ae7feef828a8ca3dd74d47c78 to your computer and use it in GitHub Desktop.
Save aparajita/9e48f40ae7feef828a8ca3dd74d47c78 to your computer and use it in GitHub Desktop.
Reformats output from @capacitor/docgen to suit my stylistic tastes
import fs from 'fs'
function indexFixer(match, index) {
// 1. Remove bullets
// 2. Remove code format from method names
// 2. Add extra \n before Interfaces
const fixedIndex = index
.replace(/^\* /ugm, '')
.replace(/\[`(.+)`\](.+)/ug, '[$1]$2')
.replace(/\[Interfaces\]/u, '\n[Interfaces]')
return `<docgen-index>${fixedIndex}</docgen-index>`
}
function fixIndex(content) {
return content.replace(/^\s*<docgen-index>(.+)<\/docgen-index>/usm, indexFixer)
}
function tableHeaderFixer(match, dashes) {
// Left align headers
return `| :${dashes} `
}
function fixTables(content) {
return content.replace(/\|\s(-+)\s/ug, tableHeaderFixer)
.replace(/\|\s*\*\*`(\w+)`\*\*\s*\|/ug, '|$1|')
.replace(/\|\s*<code>(.+?)<\/code>\s*\|/ug, '|$1|')
}
function fixReturnTypes(content) {
return content.replace(/(\*\*Returns:\*\* )<code>(.+?)<\/code>/ug, '$1$2')
}
function main() {
let content = fs.readFileSync('README.md', 'utf-8')
content = fixIndex(content)
content = fixTables(content)
content = fixReturnTypes(content)
fs.writeFileSync('README.md', content, 'utf-8')
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment