Skip to content

Commit

Permalink
feat(#45): make dead link report visibility configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
carbontwelve committed May 8, 2024
1 parent 2eac79e commit c0f8f7a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
3 changes: 3 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ type EleventyPluginInterlinkOptions = {
// slug that you are using. This defaults to a function that returns [UNABLE TO LOCATE EMBED].
unableToLocateEmbedFn?: ErrorRenderFn,

// deadLinkReport is the desired output format of the dead link report, by default its set to 'console'
deadLinkReport?: 'console' | 'json' | 'none',

// resolvingFns is a list of resolving functions. These are invoked by a wikilink containing a `:` character
// prefixed by the fn name. The page in this case is the linking page.
resolvingFns?: Map<string, (link: WikilinkMeta, currentPage: any, interlinker: Interlinker) => Promise<string>>,
Expand Down
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = function (eleventyConfig, options = {}) {
layoutKey: 'embedLayout',
layoutTemplateLangKey: 'embedLayoutLanguage',
resolvingFns: new Map(),
deadLinkReport: 'console',
}, options);

// TODO: deprecate usage of unableToLocateEmbedFn in preference of using resolving fn
Expand Down Expand Up @@ -48,7 +49,9 @@ module.exports = function (eleventyConfig, options = {}) {
// anything.
// TODO: 1.1.0 have this contain more details such as which file(s) are linking (#23)
// TODO: 1.1.0 have this clear the interlinker cache so that next time 11ty builds its starting from fresh data! (#24)
eleventyConfig.on('eleventy.after', () => interlinker.deadLinks.report());
eleventyConfig.on('eleventy.after', () => {
if (opts.deadLinkReport !== 'none') interlinker.deadLinks.report(opts.deadLinkReport)
});

// Teach Markdown-It how to display MediaWiki Links.
eleventyConfig.amendLibrary('md', (md) => {
Expand Down
29 changes: 19 additions & 10 deletions src/dead-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,26 @@ module.exports = class DeadLinks {
this.gravestones.set(link, names);
}

report() {
for (const [link, files] of this.gravestones.entries()) {
console.warn(
chalk.blue('[@photogabble/wikilinks]'),
chalk.yellow('WARNING'),
`${(link.includes('href') ? 'Link' : 'Wikilink')} (${link}) found pointing to to non-existent page in:`
);

for (const file of files) {
console.warn(`\t- ${file}`);
/**
* @param {'console'|'json'} format
*/
report(format) {
if (format === 'console') {
for (const [link, files] of this.gravestones.entries()) {
console.warn(
chalk.blue('[@photogabble/wikilinks]'),
chalk.yellow('WARNING'),
`${(link.includes('href') ? 'Link' : 'Wikilink')} (${link}) found pointing to to non-existent page in:`
);

for (const file of files) {
console.warn(`\t- ${file}`);
}
}
return;
}

const json = JSON.stringify(this.gravestones);
const n =1;
}
}
3 changes: 3 additions & 0 deletions tests/fixtures/sample-with-hash-in-title/eleventy.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(
require('../../../index.js'),
{
deadLinkReport: 'none'
}
);

return {
Expand Down

0 comments on commit c0f8f7a

Please sign in to comment.