Skip to content

Commit

Permalink
feat(src): writes plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
rockchalkwushock committed Apr 23, 2021
1 parent 796b466 commit 650b7a8
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@types/dedent": "^0.7.0",
"@types/jest": "^26.0.22",
"@types/node": "^14.14.41",
"@types/unist": "^2.0.3",
"commitizen": "^4.2.3",
"cz-conventional-changelog": "^3.3.0",
"dedent": "^0.7.0",
Expand Down Expand Up @@ -111,5 +112,8 @@
"type": "test"
}
]
},
"dependencies": {
"unist-util-visit": "^3.0.0"
}
}
49 changes: 48 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,48 @@
export const x = 42
import { Node, Parent } from 'unist'
import { visit } from 'unist-util-visit'

function rehypeCodeTitles() {
return (tree: Node) => visit(tree, 'element', visitor)

function visitor(
node: Node,
index: number | null,
parent: Parent | null
): void {
if (!parent || node.tagName !== 'pre') {
return
}

const pre = node
const code = Array.isArray(pre.children) ? pre.children[0] : pre.children
const className: Array<string | never> = code.properties.className || []

const updatedClassName = className.reduce((acc, cls) => {
// If cls is something like...
// i.e. language-typescript:lib/mdx.ts
if (cls.includes(':')) {
// Split on the ':'
const [langClassName, title] = cls.split(':')
// Add the title block to the tree at the index prior
// to the <pre /> with the title we found.
parent.children.splice(index!, 0, {
children: [{ type: 'text', value: title }],
properties: { className: ['rehype-code-title'] },
tagName: 'div',
type: 'element',
})
acc.push(langClassName)
return acc
}
if (cls.slice(0, 9) === 'language-') {
acc.push(cls)
return acc
}
return acc
}, [] as Array<string>)

pre.children = [{ ...code, properties: { className: updatedClassName } }]
}
}

export default rehypeCodeTitles
27 changes: 27 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,11 @@
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.0.tgz#7036640b4e21cc2f259ae826ce843d277dad8cff"
integrity sha512-RJJrrySY7A8havqpGObOB4W92QXKJo63/jFLLgpvOtsGUqbQZ9Sbgl35KMm1DjC6j7AvmmU2bIno+3IyEaemaw==

"@types/unist@^2.0.0", "@types/unist@^2.0.3":
version "2.0.3"
resolved "https://registry.yarnpkg.com/@types/unist/-/unist-2.0.3.tgz#9c088679876f374eb5983f150d4787aa6fb32d7e"
integrity sha512-FvUupuM3rlRsRtCN+fDudtmytGO6iHJuuRKS1Ss0pG5z8oX0diNEw94UEL7hgDbpN94rgaK5R7sWm6RrSkZuAQ==

"@types/yargs-parser@*":
version "20.2.0"
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-20.2.0.tgz#dd3e6699ba3237f0348cd085e4698780204842f9"
Expand Down Expand Up @@ -6226,6 +6231,28 @@ union-value@^1.0.0:
is-extendable "^0.1.1"
set-value "^2.0.1"

unist-util-is@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/unist-util-is/-/unist-util-is-5.0.0.tgz#c71eddea34aa7009d54f671a6fafb3779b8035d3"
integrity sha512-G4p13DhfdUNmlnJxd0uy5Skx1FG58LSDhX8h1xgpeSq0omOQ4ZN5BO54ToFlNX55NDTbRHMdwTOJXqAieInSEA==

unist-util-visit-parents@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/unist-util-visit-parents/-/unist-util-visit-parents-4.0.0.tgz#0dfc00046ac753517ffaaf59b80d1ac892b352a6"
integrity sha512-QyATSx30wHguIzI82+GVeuXGnFlh3AUVcyeZPOo5Paz2Z52zfRe3/0WLlBv6XlMWcr5xEdFqox6PteUL6hzEFA==
dependencies:
"@types/unist" "^2.0.0"
unist-util-is "^5.0.0"

unist-util-visit@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/unist-util-visit/-/unist-util-visit-3.0.0.tgz#a8e239038f7b29aadc649a6080f42bc85ea1a7bc"
integrity sha512-RbFwAZhMekKPZxlAhUIiBwim8NogwUtwLbIjvZehaGORYRDWcApsnGI1VDUHr/Jo6yW1u8VOqO/djbd7Hpz88A==
dependencies:
"@types/unist" "^2.0.0"
unist-util-is "^5.0.0"
unist-util-visit-parents "^4.0.0"

universalify@^0.1.0, universalify@^0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66"
Expand Down

0 comments on commit 650b7a8

Please sign in to comment.