Skip to content

Commit

Permalink
Merge pull request #24 from oleeskild/toc
Browse files Browse the repository at this point in the history
Implemented support for table of content
  • Loading branch information
oleeskild authored Dec 15, 2022
2 parents 71f87ad + 8da3c93 commit 5a20bf8
Show file tree
Hide file tree
Showing 12 changed files with 252 additions and 109 deletions.
49 changes: 5 additions & 44 deletions .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ const markdownIt = require("markdown-it");
const fs = require('fs');
const matter = require('gray-matter');
const faviconPlugin = require('eleventy-favicon');
const tocPlugin = require('eleventy-plugin-toc');

const {headerToId, namedHeadingsFilter} = require("./src/helpers/utils")

module.exports = function(eleventyConfig) {

let markdownLib = markdownIt({
Expand Down Expand Up @@ -184,6 +188,7 @@ module.exports = function(eleventyConfig) {

eleventyConfig.addPassthroughCopy("src/site/img");
eleventyConfig.addPlugin(faviconPlugin, { destination: 'dist' });
eleventyConfig.addPlugin(tocPlugin, {ul:true, tags: ['h1','h2', 'h3', 'h4', 'h5', 'h6']});
eleventyConfig.addFilter('jsonify', function (variable) {
return JSON.stringify(variable);
});
Expand All @@ -201,47 +206,3 @@ module.exports = function(eleventyConfig) {
};

};

function headerToId(heading) {
return slugify(heading);
}

//https://github.com/rstacruz/markdown-it-named-headings/blob/master/index.js
function namedHeadingsFilter(md, options) {
md.core.ruler.push('named_headings', namedHeadings.bind(null, md));
}

function namedHeadings(md, state) {

var ids = {}

state.tokens.forEach(function(token, i) {
if (token.type === 'heading_open') {
var text = md.renderer.render(state.tokens[i + 1].children, md.options)
var id = headerToId(text);
var uniqId = uncollide(ids, id)
ids[uniqId] = true
setAttr(token, 'id', uniqId)
}
})
}

function uncollide(ids, id) {
if (!ids[id]) return id
var i = 1
while (ids[id + '-' + i]) { i++ }
return id + '-' + i
}

function setAttr(token, attr, value, options) {
var idx = token.attrIndex(attr)

if (idx === -1) {
token.attrPush([attr, value])
} else if (options && options.append) {
token.attrs[idx][1] =
token.attrs[idx][1] + ' ' + value
} else {
token.attrs[idx][1] = value
}
}
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"axios": "^0.26.1",
"dotenv": "^10.0.0",
"eleventy-favicon": "^1.1.2",
"eleventy-plugin-toc": "^1.1.5",
"fs-file-tree": "^1.1.1",
"gray-matter": "^4.0.3",
"lunr": "^2.3.9",
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ exports.ALL_NOTE_SETTINGS= [
"dgShowLocalGraph",
"dgShowInlineTitle",
"dgShowFileTree",
"dgEnableSearch"
"dgEnableSearch",
"dgShowToc"
];
47 changes: 47 additions & 0 deletions src/helpers/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const slugify = require("@sindresorhus/slugify");

function headerToId(heading) {
return slugify(heading);
}

function namedHeadings(md, state) {

var ids = {}

state.tokens.forEach(function(token, i) {
if (token.type === 'heading_open') {
var text = md.renderer.render(state.tokens[i + 1].children, md.options)
var id = headerToId(text);
var uniqId = uncollide(ids, id)
ids[uniqId] = true
setAttr(token, 'id', uniqId)
}
})
}

function uncollide(ids, id) {
if (!ids[id]) return id
var i = 1
while (ids[id + '-' + i]) { i++ }
return id + '-' + i
}

function setAttr(token, attr, value, options) {
var idx = token.attrIndex(attr)

if (idx === -1) {
token.attrPush([attr, value])
} else if (options && options.append) {
token.attrs[idx][1] =
token.attrs[idx][1] + ' ' + value
} else {
token.attrs[idx][1] = value
}
}

//https://github.com/rstacruz/markdown-it-named-headings/blob/master/index.js
exports.namedHeadingsFilter = function (md, options) {
md.core.ruler.push('named_headings', namedHeadings.bind(null, md));
}

exports.headerToId = headerToId;
2 changes: 1 addition & 1 deletion src/site/_includes/components/graphScript.njk
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
ctx.textBaseline = 'top';
ctx.fillText(label, node.x, node.y + nodeR + 2);
})
.linkColor(() => getCssVar("--text-normal"))
.linkColor(() => getCssVar("--text-muted") || getCssVar("--text-normal"))
.graphData(gData)
.onNodeClick(node => {
window.location = node.url;
Expand Down
65 changes: 43 additions & 22 deletions src/site/_includes/components/sidebar.njk
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,53 @@
{%if settings.dgShowLocalGraph === true%}
<div class="graph">
<div style="display: flex; justify-content: flex-end;">
<div class="graph-title">Interactive graph</div>
<div class="graph-title">Connected Pages</div>
</div>
<div id="link-graph"></div>
</div>
{%endif%}

{%if settings.dgShowBacklinks === true %}
<div class="backlinks">
<div class="backlink-title" style="margin: 4px 0 !important;">Links to this page</div>
{%- if backlinks.length === 0 -%}
<div class="backlink-card">
No backlinks
{%endif%}

{%if settings.dgShowToc === true%}
{%set tocHtml= (content and (content|toc)) %}
{%if tocHtml %}
<div class="toc">
<div class="toc-title-container">
<div class="toc-title">
On this page
</div>
</div>
<div class="toc-container">
{{ tocHtml | safe }}
</div>
</div>
{%- endif -%}
{%endif%}

{%endif%}

{%if settings.dgShowBacklinks === true %}
<div class="backlinks">
<div class="backlink-title" style="margin: 4px 0 !important;">Pages mentioning this page</div>
<div class="backlink-list">

{%- if backlinks.length === 0 -%}
<div class="backlink-card">
<span class="no-backlinks-message">No other pages mentions this page</span>
</div>
{%- endif -%}

{%- for backlink in backlinks -%}
<div class="backlink-card">
<a href="{{backlink.url}}">{{backlink.title}}</a>
{%- for backlink in backlinks -%}
<div class="backlink-card">
<i class="fa fa-link"></i> <a href="{{backlink.url}}">{{backlink.title}}</a>
</div>
{%- endfor -%}

</div>
</div>
{%- endfor -%}
{%endif%}
</div>
</div>
{%endif%}
</div>
</div>
</div>

{%if settings.dgShowLocalGraph === true %}
{%include "components/graphScript.njk"%}
{% endif %}
</div>

{%if settings.dgShowLocalGraph === true %}
{%include "components/graphScript.njk"%}
{% endif %}
2 changes: 1 addition & 1 deletion src/site/_includes/layouts/note.njk
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ permalink: "notes/{{ page.fileSlug | slugify }}/"
{% endif %}
{{ content | link | highlight | safe}}

{% if settings.dgShowBacklinks === true or settings.dgShowLocalGraph === true%}
{% if settings.dgShowBacklinks === true or settings.dgShowLocalGraph === true or settings.dgShowToc === true%}
{%include "components/sidebar.njk"%}
{% endif %}
</div>
Expand Down
12 changes: 12 additions & 0 deletions src/site/index.11tydata.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ const settings = require("../helpers/constants");

const wikilink = /\[\[(.*?\|.*?)\]\]/g

const markdownIt = require("markdown-it");
const md = markdownIt({
html: true,
}).use(require("../helpers/utils").namedHeadingsFilter);

function caselessCompare(a, b) {
return a.toLowerCase() === b.toLowerCase();
}
Expand Down Expand Up @@ -111,6 +116,13 @@ module.exports = {
return currentnote.data.page.fileSlug;
}
return "";
},
content: (data) => {
const currentnote = data.collections.gardenEntry && data.collections.gardenEntry[0];
if (currentnote && currentnote.template && currentnote.template.frontMatter && currentnote.template.frontMatter.content) {
return md.render(currentnote.template.frontMatter.content);
}
return "";
}
}
}
2 changes: 1 addition & 1 deletion src/site/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
{%- endfor -%}


{%if settings.dgShowBacklinks === true or settings.dgShowLocalGraph === true%}
{% if settings.dgShowBacklinks === true or settings.dgShowLocalGraph === true or settings.dgShowToc === true%}
{%include "components/sidebar.njk" %}
{%endif%}
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/site/notes/notes.11tydata.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,6 @@ module.exports = {
noteSettings[setting] = settingValue;
});
return noteSettings;
}
}
}
}
Loading

0 comments on commit 5a20bf8

Please sign in to comment.