// 全局参数 let excerptFilter; let moreFilter; let opts; /** * 提取文章摘要, 此部分提取自 hexo-excerpt * @param {Document}post 文章 */ functionexcerpt(post) { init(); //honour the <!-- more --> !!! if ( /<!--\s*more\s*-->/.test(post.content) || post.content.indexOf('<a id="more"></a>') !== -1 ) { return post; }
let nodes = [];
let parser = new htmlparser.Parser( new htmlparser.DomHandler((err, dom) => { if (!err) { nodes = dom; } }), { decodeEntities: false, } );
parser.write(post.content); parser.done();
// tracks how many tag nodes we found let stopIndex = 1; // tracks how many nodes we found in total let index = 0; for (; index < nodes.length && stopIndex <= opts.depth; index++) { if (nodes[index].type === "tag" && excerptFilter.match(nodes[index])) { stopIndex++; } }
// set correct excerpt and more nodes values let excerptNodes = nodes.slice(0, index); let moreNodes = nodes.slice(index);
// If the hideWholePostExcerpts option is set to true (the default), don't show // excerpts for short posts (i.e. ones where the excerpt is the whole post) if (moreNodes.length != 0 || !opts.hideWholePostExcerpts) { post.excerpt = excerptNodes .map((node) => domutils.getOuterHTML(node)) .join(""); post.more = moreNodes.map((node) => domutils.getOuterHTML(node)).join(""); }
return post; }
/** * 初始化配置 */ functioninit() { const config = hexo.config; let legacy = {}; if (config.excerpt_depth) { hexo.log.warn( "excerpt_depth is deprecated, please use excerpt.depth instead." ); legacy.depth = config.excerpt_depth; }
opts = opts || defaults({}, config.excerpt, legacy, DEFAULT_CONFIG); opts.depth = parseInt(opts.depth); if (!Array.isArray(opts.excerpt_excludes)) opts.excerpt_excludes = [opts.excerpt_excludes]; if (!Array.isArray(opts.more_excludes)) opts.more_excludes = [opts.more_excludes]; excerptFilter = excerptFilter || new Filter(hexo, opts.excerpt_excludes); moreFilter = moreFilter || new Filter(hexo, opts.more_excludes); }
// generate tag index page, usually /tags/index.html if (config.tag_generator.enable_index_page) { tagDir = config.tag_dir; if (tagDir[tagDir.length - 1] !== "/") { tagDir += "/"; }