forked from imcuttle/remark-heading-id
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding option to disable default, reorganizing code, more tests
- Loading branch information
Mike Flynn
committed
Aug 17, 2023
1 parent
86a55d3
commit 7b1d28d
Showing
4 changed files
with
85 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
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(' ') | ||
} | ||
|
||
const getDefaultId = children => { | ||
return formatDefaultId(extractText(children)) | ||
} | ||
|
||
const formatDefaultId = value => { | ||
return _.kebabCase(value.replaceAll(/\\s+/g, ' ').trim()) | ||
} | ||
|
||
const setNodeId = (node, id) => { | ||
if (!node.data) node.data = {} | ||
if (!node.data.hProperties) node.data.hProperties = {} | ||
node.data.id = node.data.hProperties.id = id | ||
} | ||
|
||
module.exports = { extractText, getDefaultId, formatDefaultId, setNodeId } |