-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When authors want to use plugins for his website, he or she must modify site.json to enable them manually. There are no plugins that are enabled by default. In the future, there may be built-in MarkBind plugins that should be enabled by default. For example, we want to move the anchor functionality into a plugin. However, as there are no default plugins, MarkBind will have them disabled by default, even though anchors are a common feature in websites and it would be troublesome for authors to enable this manually. Let's add always-on plugins, plugins that are always enabled by default unless the author specify in site.json to disable them. As a "proof-of-concept", let's also refactor the anchor logic to an always-on plugin to demonstrate how always-on plugins can be utilized.
- Loading branch information
Showing
5 changed files
with
147 additions
and
41 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,40 @@ | ||
const cheerio = module.parent.require('cheerio'); | ||
const md = require('./../../lib/markbind/src/lib/markdown-it'); | ||
|
||
const ANCHOR_HTML = '<a class="fa fa-anchor" href="#"></a>'; | ||
|
||
/** | ||
* Generates a heading selector based on the indexing level | ||
* @param headingIndexingLevel to generate | ||
*/ | ||
function generateHeadingSelector(headingIndexingLevel) { | ||
let headingsSelector = 'h1'; | ||
for (let i = 2; i <= headingIndexingLevel; i += 1) { | ||
headingsSelector += `, h${i}`; | ||
} | ||
return headingsSelector; | ||
} | ||
|
||
/** | ||
* Adds anchor links to headers | ||
*/ | ||
module.exports = { | ||
postRender: (content, pluginContext, frontMatter, pageConfig) => { | ||
const $ = cheerio.load(content, { xmlMode: false }); | ||
if (pageConfig.headingIndexingLevel > 0) { | ||
const headingsSelector = generateHeadingSelector(pageConfig.headingIndexingLevel); | ||
$(headingsSelector).each((i, heading) => { | ||
$(heading).append(ANCHOR_HTML.replace('#', `#${$(heading).attr('id')}`)); | ||
}); | ||
$('panel[header]').each((i, panel) => { | ||
const panelHeading = cheerio.load(md.render(panel.attribs.header), { xmlMode: false }); | ||
if (panelHeading(headingsSelector).length >= 1) { | ||
const headingId = $(panelHeading(headingsSelector)[0]).attr('id'); | ||
const anchorIcon = ANCHOR_HTML.replace(/"/g, "'").replace('#', `#${headingId}`); | ||
$(panel).attr('header', `${$(panel).attr('header')}${anchorIcon}`); | ||
} | ||
}); | ||
} | ||
return $.html(); | ||
}, | ||
}; |
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