Skip to content

Commit

Permalink
feat: upgrade typings and behavior, release 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
crutchcorn committed Jun 24, 2024
1 parent 14fd1f3 commit ff38b3e
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 41 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ jobs:
name: ${{matrix.node}}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: dcodeIO/setup-node-nvm@master
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{matrix.node}}
- run: npm install
- run: npm test
- uses: codecov/codecov-action@v1
- uses: codecov/codecov-action@v3
strategy:
matrix:
node:
- lts/erbium
- lts/gallium
- node
20 changes: 13 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ export const slugs = new Slugger()
* enableCustomId?: boolean,
* maintainCase?: boolean,
* removeAccents?: boolean
* }} props
* }} properties
*/
export function getHeaderNodeId(node, props = {}) {
export function getHeaderNodeId(node, properties = {}) {
const {
enableCustomId = false,
maintainCase = false,
removeAccents = false
} = props
} = properties

/**
* @type {Node & HTMLElement}
Expand Down Expand Up @@ -70,19 +70,25 @@ export function getHeaderNodeId(node, props = {}) {
/**
* Plugin to add `id`s to headings.
*
* @type {import('unified').Plugin<[{
* @param {{
* enableCustomId?: boolean,
* maintainCase?: boolean,
* removeAccents?: boolean
* }], Root>}
* }} properties
*/
export default function rehypeSlug(props = {}) {
export default function rehypeSlug(properties = {}) {
/**
* @param {Root} tree
* Tree.
* @returns {undefined}
* Nothing.
*/
return (tree) => {
slugs.reset()

visit(tree, 'element', (node) => {
if (headingRank(node) && node.properties && !hasProperty(node, 'id')) {
const {id, isCustomId} = getHeaderNodeId(node, props)
const {id, isCustomId} = getHeaderNodeId(node, properties)

if (isCustomId) node.children.pop()
node.properties.id = id
Expand Down
42 changes: 22 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rehype-slug-custom-id",
"version": "1.1.0",
"version": "2.0.0",
"description": "plugin to add `id` attributes to headings similar to gatsby-remark-autolink-headers ",
"license": "MIT",
"keywords": [
Expand All @@ -24,32 +24,31 @@
"index.js"
],
"dependencies": {
"@types/hast": "^2.0.0",
"github-slugger": "^1.1.1",
"hast-util-has-property": "^2.0.0",
"hast-util-heading-rank": "^2.0.0",
"hast-util-to-string": "^2.0.0",
"@types/hast": "^3.0.4",
"github-slugger": "^2.0.0",
"hast-util-has-property": "^3.0.0",
"hast-util-heading-rank": "^3.0.0",
"hast-util-to-string": "^3.0.0",
"lodash": "^4.17.21",
"unified": "^10.0.0",
"unist-util-visit": "^4.0.0"
"unified": "^11.0.5",
"unist-util-visit": "^5.0.0"
},
"devDependencies": {
"@types/github-slugger": "^1.0.0",
"@types/lodash": "^4.14.177",
"@types/tape": "^4.0.0",
"c8": "^7.0.0",
"prettier": "^2.0.0",
"rehype": "^12.0.0",
"remark-cli": "^10.0.0",
"remark-preset-wooorm": "^9.0.0",
"rimraf": "^3.0.0",
"@types/tape": "^5.6.4",
"c8": "^10.1.2",
"prettier": "^3.3.2",
"rehype": "^13.0.1",
"remark-cli": "^12.0.1",
"remark-preset-wooorm": "^10.0.0",
"rimraf": "^5.0.7",
"tape": "^5.0.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"xo": "^0.44.0"
"typescript": "^5.5.2",
"xo": "^0.58.0"
},
"scripts": {
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
"build": "rimraf --glob \"*.d.ts\" && tsc && type-coverage",
"format": "remark . -qo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node --conditions development test.js",
"test-coverage": "c8 --check-coverage --branches 80 --functions 90 --lines 90 --statements 90 --reporter lcov npm run test-api",
Expand All @@ -64,7 +63,10 @@
"trailingComma": "none"
},
"xo": {
"prettier": true
"prettier": true,
"rules": {
"unicorn/prefer-at": "off"
}
},
"remarkConfig": {
"plugins": [
Expand Down
18 changes: 9 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ Uses [**github-slugger**][ghslug] to create GitHub style `id`s, or a custom ID i

We support the following options for the plugin:

* `enableCustomId`: `Boolean`. Enable custom header IDs with {#id} (optional)
* `maintainCase`: `Boolean`. Maintains the case for markdown header (optional)
* `removeAccents`: `Boolean`. Remove accents from generated headings IDs (optional)
* `enableCustomId`: `Boolean`. Enable custom header IDs with {#id} (optional)
* `maintainCase`: `Boolean`. Maintains the case for markdown header (optional)
* `removeAccents`: `Boolean`. Remove accents from generated headings IDs (optional)

## Security

Expand All @@ -92,12 +92,12 @@ Always be wary with user input and use [`rehype-sanitize`][sanitize].

## Related

* [`rehype-slug`](https://github.com/rehypejs/rehype-slug)
— Add slugs to headings in html
* [`remark-slug`](https://github.com/wooorm/remark-slug)
— Add slugs to headings in markdown
* [`gatsby-remark-autolink-headers`](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-remark-autolink-headers)
— Add slugs to headings in markdown for Gatsby
* [`rehype-slug`](https://github.com/rehypejs/rehype-slug)
— Add slugs to headings in html
* [`remark-slug`](https://github.com/wooorm/remark-slug)
— Add slugs to headings in markdown
* [`gatsby-remark-autolink-headers`](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-remark-autolink-headers)
— Add slugs to headings in markdown for Gatsby

<!-- Definitions -->

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"include": ["*.js"],
"compilerOptions": {
"target": "ES2020",
"lib": ["ES2020", "DOM"],
"lib": ["ES2020", "DOM", "ES2020.String"],
"module": "ES2020",
"moduleResolution": "node",
"allowJs": true,
Expand Down

0 comments on commit ff38b3e

Please sign in to comment.