mirror of
https://github.com/nature-of-code/noc-book-2
synced 2024-11-17 07:49:05 +01:00
43 lines
1 KiB
JavaScript
43 lines
1 KiB
JavaScript
const through = require('through2');
|
|
const cheerio = require('cheerio');
|
|
|
|
const Plugin = function (registry) {
|
|
registry.after(
|
|
'markdown:convert',
|
|
'relativelink:href',
|
|
this.replaceRelativeHref,
|
|
);
|
|
};
|
|
|
|
Plugin.prototype = {
|
|
replaceRelativeHref: function (config, stream, extras, callback) {
|
|
stream = stream.pipe(
|
|
through.obj(function (file, _, cb) {
|
|
file.$el = file.$el || cheerio.load(file.contents.toString());
|
|
let found = false;
|
|
|
|
file.$el('a').each(function () {
|
|
const jel = file.$el(this);
|
|
const prefHref = jel.attr('href');
|
|
|
|
// test if is a valid relative link
|
|
if (prefHref && prefHref.indexOf('://') < 0) {
|
|
// remove the relative path
|
|
jel.attr('href', `#${prefHref.split('#')[1]}`);
|
|
found = true;
|
|
}
|
|
});
|
|
|
|
if (found) {
|
|
file.contents = Buffer.from(file.$el.html());
|
|
}
|
|
|
|
cb(null, file);
|
|
}),
|
|
);
|
|
|
|
callback(null, config, stream, extras);
|
|
},
|
|
};
|
|
|
|
module.exports = Plugin;
|