Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config option for floatIcon #36

Merged
merged 3 commits into from
Sep 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions assets/lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const defaultConfig = {
mode: "float",
showGoTop: true, //是否显示返回顶部摁扭
float: { //浮动导航设置
floatIcon: "fa fa-navicon",
showLevelIcon: false, //是否显示层级图标
level1Icon: "fa fa-hand-o-right",
level2Icon: "fa fa-hand-o-right",
Expand Down
31 changes: 22 additions & 9 deletions assets/lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ var Config = require('./config.js');
* @param page
* @returns {Array} 返回处理好的tocs合集
*/
function handlerTocs($, page) {
function handlerTocs($, page, modifyHeader) {
var config = Config.config;
var tocs = [];

var count = {
h1: 0,
h2: 0,
Expand All @@ -27,13 +28,13 @@ function handlerTocs($, page) {
if (id) {
switch (elem.tagName) {
case "h1":
handlerH1Toc(config, count, header, tocs, page.level);
handlerH1Toc(config, count, header, tocs, page.level, modifyHeader);
break;
case "h2":
handlerH2Toc(config, count, header, tocs, page.level);
handlerH2Toc(config, count, header, tocs, page.level, modifyHeader);
break;
case "h3":
handlerH3Toc(config, count, header, tocs, page.level);
handlerH3Toc(config, count, header, tocs, page.level, modifyHeader);
break;
default:
titleAddAnchor(header, id);
Expand Down Expand Up @@ -82,7 +83,7 @@ function titleAddAnchor(header, id) {
* @param header
* @param tocs 根节点
*/
function handlerH1Toc(config, count, header, tocs, pageLevel) {
function handlerH1Toc(config, count, header, tocs, pageLevel, modifyHeader) {
var title = header.text();
var id = header.attr('id');
var level = ''; //层级
Expand All @@ -101,6 +102,9 @@ function handlerH1Toc(config, count, header, tocs, pageLevel) {
if (config.associatedWithSummary && config.themeDefault.showLevel) {
level = pageLevel + '.' + level;
}
if (!modifyHeader) {
level = '';
}
header.text(level + title); //重写标题
}
titleAddAnchor(header, id);
Expand All @@ -117,7 +121,7 @@ function handlerH1Toc(config, count, header, tocs, pageLevel) {
* @param count 计数器
* @param header
*/
function handlerH2Toc(config, count, header, tocs, pageLevel) {
function handlerH2Toc(config, count, header, tocs, pageLevel, modifyHeader) {
var title = header.text();
var id = header.attr('id');
var level = ''; //层级
Expand All @@ -140,6 +144,9 @@ function handlerH2Toc(config, count, header, tocs, pageLevel) {
if (config.associatedWithSummary && config.themeDefault.showLevel) {
level = pageLevel + '.' + level;
}
if (!modifyHeader) {
level = '';
}
header.text(level + title); //重写标题
}
titleAddAnchor(header, id);
Expand All @@ -156,7 +163,7 @@ function handlerH2Toc(config, count, header, tocs, pageLevel) {
* @param count 计数器
* @param header
*/
function handlerH3Toc(config, count, header, tocs, pageLevel) {
function handlerH3Toc(config, count, header, tocs, pageLevel, modifyHeader) {
var title = header.text();
var id = header.attr('id');
var level = ''; //层级
Expand Down Expand Up @@ -184,6 +191,9 @@ function handlerH3Toc(config, count, header, tocs, pageLevel) {
if (config.associatedWithSummary && config.themeDefault.showLevel) {
level = pageLevel + "." + level;
}
if (!modifyHeader) {
level = '';
}
header.text(level + title); //重写标题
}
titleAddAnchor(header, id);
Expand All @@ -203,6 +213,7 @@ function handlerH3Toc(config, count, header, tocs, pageLevel) {
function handlerFloatNavbar($, tocs) {
var config = Config.config;
var float = config.float;
var floatIcon = float.floatIcon;
var level1Icon = '';
var level2Icon = '';
var level3Icon = '';
Expand All @@ -212,7 +223,7 @@ function handlerFloatNavbar($, tocs) {
level3Icon = float.level3Icon;
}

var html = "<div id='anchor-navigation-ex-navbar'><i class='fa fa-anchor'></i><ul>";
var html = "<div id='anchor-navigation-ex-navbar'><i class='" + floatIcon + "'></i><ul>";
for (var i = 0; i < tocs.length; i++) {
var h1Toc = tocs[i];
html += "<li><span class='title-icon " + level1Icon + "'></span><a href='#" + h1Toc.url + "'><b>" + h1Toc.level + "</b>" + h1Toc.name + "</a></li>";
Expand Down Expand Up @@ -296,8 +307,10 @@ function buildGoTop(tocs) {

function start(bookIns, page) {
var $ = cheerio.load(page.content);
var modifyHeader = !/<!--[ \t]*ex_nolevel[ \t]*-->/.test(page.content)

// 处理toc相关,同时处理标题和id
var tocs = handlerTocs($, page);
var tocs = handlerTocs($, page, modifyHeader);

// 设置处理之后的内容
if (tocs.length == 0) {
Expand Down