Skip to content

Commit

Permalink
feat: adding default header & allowing prettier to be run on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Flynn committed Aug 15, 2023
1 parent d17e306 commit 86a55d3
Show file tree
Hide file tree
Showing 5 changed files with 5,545 additions and 3,864 deletions.
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,32 @@
[![Prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://prettier.io/)
[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg?style=flat-square)](https://conventionalcommits.org)

> The remark plugin for supporting [custom heading id](https://www.markdownguide.org/extended-syntax/#heading-ids)
> The remark plugin for supporting [custom heading id](https://www.markdownguide.org/extended-syntax/#heading-ids) and Default Id
### Input
### Custom Heading Input

```markdown
### My Great Heading {#custom-id}
```

### Output
### Custom Heading Output

```html
<h3 id="custom-id">My Great Heading</h3>
```

### Default Heading Input

```markdown
### My Great Heading
```

### Default Heading Output

```html
<h3 id="my-great-heading">My Great Heading</h3>
```

## Contributing

- Fork it!
Expand Down
26 changes: 14 additions & 12 deletions __tests__/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@ describe('remarkHeadingId', function() {
.use(html).processSync(`# head
# cus head1 {#idd-id}
# cus head2 {#idd id}
# cus head3 {#中文 id}
`)
# cus head3 {#中文 id}`)

expect(contents).toMatchInlineSnapshot(`
"<h1>head</h1>
"<h1 id=\\"head\\">head</h1>
<h1 id=\\"idd-id\\">cus head1</h1>
<h1 id=\\"idd id\\">cus head2</h1>
<h1 id=\\"中文 id\\">cus head3</h1>
<pre><code>
</code></pre>"
<h1 id=\\"中文 id\\">cus head3</h1>"
`)
})

Expand All @@ -40,17 +37,22 @@ describe('remarkHeadingId', function() {
.use(remarkHeadingId)
.use(stringify)
.use(html).processSync(`
# head \`heada\`
# My Great Heading
## head **headb**
## head ~~headc~~
# cus \`head1\` {#idd-id}
## cus **head2** {#idd id}
## cus ~~head2~~ {#idd id}
`)
## cus ~~head2~~ {#idd id}`)

expect(contents).toMatchInlineSnapshot(`
"<h1 id=\\"idd-id\\">cus <code>head1</code></h1>
"<h1 id=\\"head-heada\\">head <code>heada</code></h1>
<h1 id=\\"my-great-heading\\">My Great Heading</h1>
<h2 id=\\"head-headb\\">head <strong>headb</strong></h2>
<h2 id=\\"head-headc\\">head <del>headc</del></h2>
<h1 id=\\"idd-id\\">cus <code>head1</code></h1>
<h2 id=\\"idd id\\">cus <strong>head2</strong></h2>
<h2 id=\\"idd id\\">cus <del>head2</del> </h2>
<pre><code>
</code></pre>"
<h2 id=\\"idd id\\">cus <del>head2</del> </h2>"
`)
})
})
31 changes: 24 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
/**
* The remark plugin for supporting custom id
* The remark plugin for supporting custom id and default id
* @author imcuttle
*/

const visit = require('unist-util-visit')
const _ = require('lodash')

const extractText = children => {
return children
.map(child => {
if (!_.isEmpty(child.value)) {
return child.value
} else if (child.children && child.children.length > 0) {
return extractText(child.children)
} else {
return ''
}
})
.join(' ')
}

module.exports = function() {
return function(node) {
visit(node, 'heading', node => {
if (!node.data) node.data = {}
if (!node.data.hProperties) node.data.hProperties = {}

let lastChild = node.children[node.children.length - 1]
if (lastChild && lastChild.type === 'text') {
let string = lastChild.value.replace(/ +$/, '')
Expand All @@ -16,19 +34,18 @@ module.exports = function() {
if (matched) {
let id = matched[1]
if (!!id.length) {
if (!node.data) {
node.data = {}
}
if (!node.data.hProperties) {
node.data.hProperties = {}
}
node.data.id = node.data.hProperties.id = id

string = string.substring(0, matched.index)
lastChild.value = string
return
}
}
}

// If no custom id was found, use default instead
let fullText = extractText(node.children)
node.data.id = node.data.hProperties.id = _.kebabCase(fullText)
})
}
}
Loading

0 comments on commit 86a55d3

Please # to comment.