-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
75 lines (63 loc) · 3.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import {install} from './src/markdown-ext.js';
import Interlinker from './src/interlinker.js';
import {defaultResolvingFn, defaultEmbedFn} from './src/resolvers.js';
/**
* Some code borrowed from:
* @see https://git.sr.ht/~boehs/site/tree/master/item/html/pages/garden/garden.11tydata.js
* @see https://github.com/oleeskild/digitalgarden/blob/main/src/helpers/linkUtils.js
*
* @param { import('@11ty/eleventy/src/UserConfig') } eleventyConfig
* @param { import('@photogabble/eleventy-plugin-interlinker').EleventyPluginInterlinkOptions } options
*/
export default function (eleventyConfig, options = {}) {
/** @var { import('@photogabble/eleventy-plugin-interlinker').EleventyPluginInterlinkOptions } opts */
const opts = Object.assign({
defaultLayout: null,
defaultLayoutLang: null,
layoutKey: 'embedLayout',
layoutTemplateLangKey: 'embedLayoutLanguage',
resolvingFns: new Map(),
deadLinkReport: 'console',
}, options);
// TODO: deprecate usage of unableToLocateEmbedFn in preference of using resolving fn
if (typeof opts.unableToLocateEmbedFn === 'function' && !opts.resolvingFns.has('404-embed')) {
opts.resolvingFns.set('404-embed', async (link) => opts.unableToLocateEmbedFn(link.page.fileSlug));
}
// Set default stub url if not set by author.
if (typeof opts.stubUrl === 'undefined') opts.stubUrl = '/stubs/';
// Default resolving functions for converting a Wikilink into HTML.
if (!opts.resolvingFns.has('default')) opts.resolvingFns.set('default', defaultResolvingFn);
if (!opts.resolvingFns.has('default-embed')) opts.resolvingFns.set('default-embed', defaultEmbedFn);
if (!opts.resolvingFns.has('404-embed')) opts.resolvingFns.set('404-embed', async () => '[UNABLE TO LOCATE EMBED]');
const interlinker = new Interlinker(opts);
// This populates templateConfig with an instance of TemplateConfig once 11ty has initiated it, it's
// used by the template compiler function that's exported by the EleventyRenderPlugin and used
// by the defaultEmbedFn resolving function for compiling embed templates.
eleventyConfig.on("eleventy.config", (cfg) => {
interlinker.templateConfig = cfg;
});
// This triggers on an undocumented internal 11ty event that is triggered once EleventyExtensionMap
// has been loaded. This is used by the EleventyRenderPlugin which is made user of within the
// compileTemplate method of the Interlinker class.
eleventyConfig.on("eleventy.extensionmap", (map) => {
interlinker.extensionMap = map;
});
// After 11ty has finished generating the site output a list of wikilinks that do not link to
// anything.
eleventyConfig.on('eleventy.after', () => {
if (opts.deadLinkReport !== 'none') interlinker.deadLinks.report(opts.deadLinkReport);
});
// Reset the internal state of the interlinker if running in watch mode, this stops
// the interlinker from working against out of date data.
eleventyConfig.on('eleventy.beforeWatch', () => {
interlinker.reset();
});
// Teach Markdown-It how to display MediaWiki Links.
eleventyConfig.amendLibrary('md', (md) => install(md, interlinker.wikiLinkParser));
// Add outboundLinks computed global data, this is executed before the templates are compiled and
// thus markdown parsed.
eleventyConfig.addGlobalData('eleventyComputed.outboundLinks', () => {
return async (data) => interlinker.compute(data);
});
// TODO: 1.1.0 Make Interlinker class available via global data
};