1 Star 0 Fork 34

liming0101/outline.js

forked from Yaohaixiao/outline.js 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
getChapters.js 1.36 KB
一键复制 编辑 原始数据 按行查看 历史
import isString from './utils/types/isString'
import isFunction from './utils/types/isFunction'
import isElement from './utils/types/isElement'
import toTree from './utils/lang/toTree'
import getChaptersByHeadings from './getChaptersByHeadings'
/**
*
* @param {Object} [options]
* @param {String|HTMLElement} [options.articleElement]
* @param {String} [options.selector]
* @param {Boolean} [options.showCode]
* @param {Function} [options.chapterTextFilter]
* @param {Boolean} [options.isTreeStructured]
* @returns {Array}
*/
const getChapters = (options) => {
const {
articleElement,
selector,
showCode,
chapterTextFilter,
isTreeStructured
} = options
let $article = document.querySelector('#article')
let $headings = []
let chapters = []
let filter = (text) => {
return text.replace(/\(.*?\)/, '()')
}
if (isString(articleElement)) {
$article = document.querySelector(articleElement)
} else if (isElement(articleElement)) {
$article = articleElement
}
if (!$article) {
return chapters
}
if (isFunction(chapterTextFilter)) {
filter = chapterTextFilter
}
$headings = [...$article.querySelectorAll(selector || 'h1,h2,h3,h4,h5,h6')]
chapters = getChaptersByHeadings($headings, showCode || true, filter)
return isTreeStructured === true ? toTree(chapters, 'id', 'pid') : chapters
}
export default getChapters
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/liming0101/outline.js.git
git@gitee.com:liming0101/outline.js.git
liming0101
outline.js
outline.js
master

搜索帮助