Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 6, 2021
1 parent e8f70d5 commit daa914f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 29 deletions.
26 changes: 11 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,20 @@ import BananaSlug from 'github-slugger'

const slugs = new BananaSlug()

export default function remarkSlug() {
return transformer
}

// Patch slugs on heading nodes.
function transformer(ast) {
slugs.reset()

visit(ast, 'heading', visitor)
export default function remarkSlug() {
return (ast) => {
slugs.reset()

function visitor(node) {
var data = node.data || (node.data = {})
var props = data.hProperties || (data.hProperties = {})
var id = props.id
visit(ast, 'heading', (node) => {
const data = node.data || (node.data = {})
const props = data.hProperties || (data.hProperties = {})
let id = props.id

id = id ? slugs.slug(id, true) : slugs.slug(toString(node))
id = id ? slugs.slug(id, true) : slugs.slug(toString(node))

data.id = id
props.id = id
data.id = id
props.id = id
})
}
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"tape": "^5.0.0",
"unist-builder": "^3.0.0",
"unist-util-remove-position": "^4.0.0",
"xo": "^0.37.0"
"xo": "^0.39.0"
},
"scripts": {
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
Expand All @@ -65,7 +65,6 @@
},
"xo": {
"prettier": true,
"esnext": false,
"ignores": [
"types/index.d.ts"
]
Expand Down
21 changes: 9 additions & 12 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {u} from 'unist-builder'
import {removePosition} from 'unist-util-remove-position'
import remarkSlug from './index.js'

test('remarkSlug', function (t) {
test('remarkSlug', (t) => {
t.deepEqual(
removePosition(
remark()
Expand Down Expand Up @@ -39,9 +39,8 @@ test('remarkSlug', function (t) {
t.deepEqual(
removePosition(
remark()
.use(function () {
return transform
function transform(tree) {
.use(() => {
return (tree) => {
tree.children[0].data = {foo: 'bar'}
}
})
Expand All @@ -65,9 +64,8 @@ test('remarkSlug', function (t) {
t.deepEqual(
removePosition(
remark()
.use(function () {
return transform
function transform(tree) {
.use(() => {
return (tree) => {
tree.children[0].data = {hProperties: {className: ['foo']}}
}
})
Expand All @@ -91,9 +89,8 @@ test('remarkSlug', function (t) {
t.deepEqual(
removePosition(
remark()
.use(function () {
return transform
function transform(tree) {
.use(() => {
return (tree) => {
tree.children[1].data = {hProperties: {id: 'here'}}
tree.children[3].data = {hProperties: {id: 'something'}}
}
Expand Down Expand Up @@ -221,12 +218,12 @@ test('remarkSlug', function (t) {
function heading(label, id) {
return u(
'heading',
{depth: 2, data: {id: id, hProperties: {id: id}}},
{depth: 2, data: {id, hProperties: {id}}},
label ? [u('text', label)] : []
)
}

function process(doc, options) {
var processor = remark().use(remarkSlug, options)
const processor = remark().use(remarkSlug, options)
return removePosition(processor.runSync(processor.parse(doc)), true)
}

0 comments on commit daa914f

Please sign in to comment.