-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from ncoden/feat/ignore-html-in-headings
feat: ignore HTML in headings for the table of content
- Loading branch information
Showing
4 changed files
with
57 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
var Cheerio = require('cheerio'); | ||
|
||
/** | ||
* Return the top-level text in the given string or jQuery element without | ||
* the nested HTML. | ||
* | ||
* ```js | ||
* stripHtml('hello world') // -> "hello" | ||
* stripHtml('hello <b>world</b>') // -> "hello" | ||
* ``` | ||
*/ | ||
module.exports = function (param) { | ||
var $el; | ||
|
||
if (typeof param === 'string') | ||
$el = Cheerio.load(param)('body'); | ||
else if (typeof param === 'object') | ||
$el = $el.clone(); | ||
else | ||
throw new Error('expected a string or a jQuery object, got "' + param + '" (' + typeof param + ')'); | ||
|
||
// https://stackoverflow.com/a/33592275 | ||
var $topLevelEl = $el.children().remove().end(); | ||
var text = $topLevelEl.text(); | ||
|
||
return text; | ||
} |
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