Skip to content

Commit 112a60e

Browse files
committed
chore(build): moving TOC headlines from metalsmith to markdown
1 parent b8e76de commit 112a60e

File tree

6 files changed

+18
-43
lines changed

6 files changed

+18
-43
lines changed

build/metalsmith/assets/website.css

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,13 @@ main > article:first-of-type > header > h1 {
230230
}
231231
main .link-to-headline {
232232
text-decoration: none;
233-
font-size: 65%;
234233
font-weight: normal;
235234
display: inline-block;
236-
margin-right: 6px;
235+
color: #999999;
236+
}
237+
main .link-to-headline:hover,
238+
main .link-to-headline:focus {
239+
text-decoration: underline;
237240
}
238241

239242

build/metalsmith/metalsmith.markdown.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ const markdownOptions = {
55
html: true,
66
};
77

8+
const toc = require('markdown-it-toc-and-anchor').default;
9+
const tocOptions = {
10+
// we are only interested in the headline mutations,
11+
// as the TOC is built by a metalsmith plugin
12+
toc: false,
13+
anchorClassName: 'link-to-headline',
14+
};
15+
816
// see https://allyjs.io/contributing/docs.html#Notes-and-warnings
917
const container = require('markdown-it-container');
1018
const containerOptions = {
@@ -29,5 +37,6 @@ const deflist = require('markdown-it-deflist');
2937
module.exports = function() {
3038
return markdown(markdownOptions)
3139
.use(container, 'note', containerOptions)
32-
.use(deflist);
40+
.use(deflist)
41+
.use(toc, tocOptions);
3342
};

build/metalsmith/plugins/inject-examples.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
const path = require('path');
33
const URI = require('urijs');
44
const cheerio = require('cheerio');
5-
const utils = require('./utils.js');
65

76
function transform($, data, examples) {
87
$('ul li strong').each(function() {
@@ -28,7 +27,6 @@ function transform($, data, examples) {
2827

2928
const title = String($example('h1').first().text());
3029
const $title = $('<h1>').text('Example: ' + title);
31-
utils.makeHeadlineLinkable($title, $);
3230

3331
const jsbin = String($example('link[rel="jsbin"]').attr('href'));
3432
const jsbinEmbed = new URI(jsbin).segment('embed').query('output').toString();

build/metalsmith/plugins/prepare.transform.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11

22
'use strict';
33

4-
const utils = require('./utils.js');
5-
64
function extractData($, data) {
75
// extract h1 to title
86
if (!data.title) {
9-
data.title = $('h1').text();
7+
data.title = $('h1').text().replace(/^#\s+/, '');
108
data.titleUrlEncoded = encodeURIComponent(data.title);
119
}
1210

@@ -51,12 +49,6 @@ function removeEmptyApiSections($/*, data*/) {
5149
});
5250
}
5351

54-
function makeHeadlinesLinkable($/*, data*/) {
55-
$('h1, h2, h3, h4, h5, h6').each(function() {
56-
utils.makeHeadlineLinkable($(this), $);
57-
});
58-
}
59-
6052
function extractTableOfContents($, data) {
6153
// prepare Table Of Contents
6254
// inlining what metalsmith-autotoc would've done
@@ -65,7 +57,7 @@ function extractTableOfContents($, data) {
6557
$('h2').each(function() {
6658
const $this = $(this).clone();
6759
const $link = $this.find('.link-to-headline');
68-
const id = $link.attr('id');
60+
const id = $link.parent().attr('id');
6961
$link.remove();
7062
const text = $this.text();
7163

@@ -93,7 +85,6 @@ module.exports = function($, data) {
9385
rewriteUrlsFromMdToHtml($, data);
9486

9587
removeEmptyApiSections($, data);
96-
makeHeadlinesLinkable($, data);
9788
extractTableOfContents($, data);
9889

9990
convertCodeLanguageForPrism($, data);

build/metalsmith/plugins/utils.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
"markdown-it": "^8.0.0",
115115
"markdown-it-container": "^2.0.0",
116116
"markdown-it-deflist": "^2.0.1",
117+
"markdown-it-toc-and-anchor": "^4.1.1",
117118
"markdownlint": "^0.2.0",
118119
"metalsmith": "^2.1.0",
119120
"metalsmith-broken-link-checker": "^0.1.8",

0 commit comments

Comments
 (0)