-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 78ff0f8
Showing
9 changed files
with
4,419 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 2 | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules/ | ||
npm-debug.log | ||
coverage/ | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Changelog | ||
|
||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), | ||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). | ||
|
||
## [Unreleased] | ||
|
||
## [1.0.0] | ||
|
||
First release | ||
|
||
[1.0.0]: https://github.com/photogabble/eleventy-plugin-font-subsetting/releases/tag/v1.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 PhotoGabble | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
type EleventyPluginInterlinkOptions = { | ||
// defaultLayout is the optional default layout you would like to use for wrapping your embeds. | ||
defaultLayout?: string, | ||
|
||
// layoutKey is the front-matter value used for a per embed template, if found it will replace defaultLayout for | ||
// that embed. This will always default to `embedLayout`. | ||
layoutKey?: string, | ||
|
||
// unableToLocateEmbedFn is invoked when an embed is unable to be found, this is normally due to a typo in the | ||
// slug that you are using. This defaults to a function that returns [UNABLE TO LOCATE EMBED]. | ||
unableToLocateEmbedFn?: ErrorRenderFn, | ||
|
||
// slugifyFn is used to slugify strings. If a function isn't set then the default 11ty slugify filter is used. | ||
slugifyFn?: SlugifyFn | ||
} | ||
|
||
interface ErrorRenderFn { | ||
(slug: string): string; | ||
} | ||
|
||
interface SlugifyFn { | ||
(input: string): string; | ||
} | ||
|
||
export {EleventyPluginInterlinkOptions}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
const { EleventyRenderPlugin } = require("@11ty/eleventy"); | ||
const chalk = require("chalk"); | ||
|
||
/** | ||
* Some code borrowed from: | ||
* @see https://git.sr.ht/~boehs/site/tree/master/item/html/pages/garden/garden.11tydata.js | ||
* | ||
* @param { import('@11ty/eleventy/src/UserConfig') } eleventyConfig | ||
* @param { import('@photogabble/eleventy-plugin-interlinker').EleventyPluginInterlinkOptions } options | ||
*/ | ||
module.exports = function (eleventyConfig, options = {}) { | ||
/** @var { import('@photogabble/eleventy-plugin-interlinker').EleventyPluginInterlinkOptions } opts */ | ||
const opts = Object.assign({ | ||
defaultLayout: null, | ||
layoutKey: 'embedLayout', | ||
unableToLocateEmbedFn: () => '[UNABLE TO LOCATE EMBED]', | ||
slugifyFn: (input) => { | ||
const slugify = eleventyConfig.getFilter('slugify'); | ||
if(typeof slugify !== 'function') throw new Error('Unable to load slugify filter.'); | ||
|
||
return slugify(input); | ||
}, | ||
}, options); | ||
|
||
const rm = new EleventyRenderPlugin.RenderManager(); | ||
|
||
// This regex finds all WikiLink style links: [[id|optional text]] as well as WikiLink style embeds: ![[id]] | ||
const wikiLinkRegExp = /(?<!!)(!?)\[\[([^|]+?)(\|([\s\S]+?))?\]\]/g; | ||
|
||
const parseWikiLink = (link) => { | ||
const isEmbed = link.startsWith('!'); | ||
const parts = link.slice((isEmbed ? 3 : 2), -2).split("|").map(part => part.trim()); | ||
const slug = opts.slugifyFn(parts[0].replace(/.(md|markdown)\s?$/i, "").trim()); | ||
|
||
return { | ||
title: parts.length === 2 ? parts[1] : null, | ||
name: parts[0], | ||
link, | ||
slug, | ||
isEmbed | ||
} | ||
}; | ||
|
||
const parseWikiLinks = (arr) => arr.map(link => parseWikiLink(link)); | ||
|
||
const compileTemplate = async (data) => { | ||
if (compiledEmbeds.has(data.inputPath)) return; | ||
|
||
const frontMatter = data.template.frontMatter; | ||
|
||
const layout = (data.data.hasOwnProperty(opts.layoutKey)) | ||
? data.data[opts.layoutKey] | ||
: opts.defaultLayout; | ||
|
||
const tpl = layout === null | ||
? frontMatter.content | ||
: `{% layout "${layout}" %} {% block content %} ${frontMatter.content} {% endblock %}`; | ||
|
||
const fn = await rm.compile(tpl, data.page.templateSyntax, {templateConfig, extensionMap}); | ||
const result = await fn(data.data); | ||
|
||
compiledEmbeds.set(data.inputPath, result); | ||
} | ||
|
||
let templateConfig; | ||
eleventyConfig.on("eleventy.config", (cfg) => { | ||
templateConfig = cfg; | ||
}); | ||
|
||
let extensionMap; | ||
eleventyConfig.on("eleventy.extensionmap", (map) => { | ||
extensionMap = map; | ||
}); | ||
|
||
// Set of WikiLinks pointing to non-existent pages | ||
const deadWikiLinks = new Set(); | ||
|
||
// Map of what WikiLinks to what | ||
const linkMapCache = new Map(); | ||
|
||
// Map of WikiLinks that have triggered an embed compile | ||
const compiledEmbeds = new Map(); | ||
|
||
eleventyConfig.on('eleventy.after', () => { | ||
deadWikiLinks.forEach( | ||
slug => console.warn(chalk.blue('[@photogabble/wikilinks]'), chalk.yellow('WARNING'), `WikiLink found pointing to non-existent [${slug}], has been set to default stub.`) | ||
); | ||
}); | ||
|
||
// Teach Markdown-It how to display MediaWiki Links. | ||
eleventyConfig.amendLibrary('md', (md) => { | ||
// WikiLink Embed | ||
md.inline.ruler.push('inline_wikilink_embed', (state, silent) => { | ||
// Have we found the start of a WikiLink Embed `![[` | ||
if (state.src.charAt(state.pos) === '!' && state.src.substring(state.pos, state.pos + 3) === '![[') { | ||
if (!silent) { | ||
const token = state.push('inline_wikilink_embed', '', 0) | ||
const wikiLink = parseWikiLink(state.src); | ||
token.content = wikiLink.slug; | ||
state.pos = state.posMax; | ||
} | ||
return true; | ||
} | ||
}); | ||
|
||
md.renderer.rules.inline_wikilink_embed = (tokens, idx) => { | ||
const token = tokens[idx]; | ||
const link = linkMapCache.get(token.content); | ||
if (!link) { | ||
console.error(chalk.blue('[@photogabble/wikilinks]'), chalk.red('ERROR'), `WikiLink Embed found pointing to non-existent [${token.content}], doesn't exist.`); | ||
return (typeof opts.unableToLocateEmbedFn === 'function') | ||
? opts.unableToLocateEmbedFn(token.content) | ||
: ''; | ||
} | ||
|
||
const templateContent = compiledEmbeds.get(link.page.inputPath); | ||
if (!templateContent) throw new Error(`WikiLink Embed found pointing to [${token.content}], has no compiled template.`); | ||
|
||
return compiledEmbeds.get(link.page.inputPath); | ||
} | ||
|
||
// WikiLink via linkify | ||
md.linkify.add("[[", { | ||
validate: /^\s?([^\[\]\|\n\r]+)(\|[^\[\]\|\n\r]+)?\s?\]\]/, | ||
normalize: match => { | ||
const wikiLink = parseWikiLink(match.raw); | ||
const found = linkMapCache.get(wikiLink.slug); | ||
|
||
if (!found) { | ||
deadWikiLinks.add(wikiLink.slug); | ||
match.text = wikiLink.title ?? wikiLink.name; | ||
match.url = '/stubs'; | ||
return; | ||
} | ||
|
||
match.text = wikiLink.title ?? found.title; | ||
match.url = found.page.url; | ||
} | ||
}); | ||
}); | ||
|
||
// Add backlinks computed global data, this is executed before the templates are compiled and thus markdown parsed. | ||
eleventyConfig.addGlobalData('eleventyComputed', { | ||
backlinks: async (data) => { | ||
// @see https://www.11ty.dev/docs/data-computed/#declaring-your-dependencies | ||
const dependencies = [data.title, data.page, data.collections.all]; | ||
if (dependencies[0] === undefined || !dependencies[1].fileSlug || dependencies[2].length === 0) return []; | ||
|
||
const compilePromises = []; | ||
const allPages = data.collections.all; | ||
const currentSlug = opts.slugifyFn(data.title); | ||
let backlinks = []; | ||
let currentSlugs = new Set([currentSlug, data.page.fileSlug]); | ||
const currentPage = allPages.find(page => page.inputPath === data.page.inputPath); | ||
|
||
// Populate our link map for use later in replacing WikiLinks with page permalinks. | ||
// Pages can list aliases in their front matter, if those exist we should map them | ||
// as well. | ||
|
||
linkMapCache.set(currentSlug, { | ||
page: data.collections.all.find(page => page.inputPath === data.page.inputPath), | ||
title: data.title | ||
}); | ||
|
||
// If a page has defined aliases, then add those to the link map. These must be unique. | ||
|
||
if (data.aliases) { | ||
for (const alias of data.aliases) { | ||
const aliasSlug = opts.slugifyFn(alias); | ||
linkMapCache.set(aliasSlug, { | ||
page: currentPage, | ||
title: alias | ||
}); | ||
currentSlugs.add(aliasSlug) | ||
} | ||
} | ||
|
||
// Loop over all pages and build their outbound links if they have not already been parsed. | ||
allPages.forEach(page => { | ||
if (!page.data.outboundLinks) { | ||
const pageContent = page.template.frontMatter.content; | ||
const outboundLinks = (pageContent.match(wikiLinkRegExp) || []); | ||
page.data.outboundLinks = parseWikiLinks(outboundLinks); | ||
|
||
page.data.outboundLinks | ||
.filter(link => link.isEmbed && compiledEmbeds.has(link.slug) === false) | ||
.map(link => allPages.find(page => { | ||
const found = (page.fileSlug === link.slug || (page.data.title && opts.slugifyFn(page.data.title) === link.slug)); | ||
if (found) return true; | ||
|
||
const aliases = (page.aliases ?? []).reduce(function(set, alias){ | ||
set.add(opts.slugifyFn(alias)); | ||
return set; | ||
}, new Set()); | ||
|
||
return aliases.has(link.slug); | ||
})) | ||
.filter(link => (typeof link !== undefined)) | ||
.forEach(link => compilePromises.push(compileTemplate(link))) | ||
} | ||
|
||
// If the page links to our current page either by its title or by its aliases then | ||
// add that page to our current page's backlinks. | ||
if (page.data.outboundLinks.some(link => currentSlugs.has(link.slug))) { | ||
backlinks.push({ | ||
url: page.url, | ||
title: page.data.title, | ||
}) | ||
} | ||
}); | ||
|
||
// Block iteration until compilation complete. | ||
if (compilePromises.length > 0) await Promise.all(compilePromises); | ||
|
||
// The backlinks for the current page. | ||
return backlinks; | ||
} | ||
}); | ||
}; |
Oops, something went wrong.