eleventy-plugin-backlinks
Advanced tools
Comparing version 0.2.1 to 0.3.0
@@ -5,3 +5,5 @@ # Changelog | ||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project tries to adhere to [Semantic Versioning (SemVer)](https://semver.org/spec/v2.0.0.html). | ||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), | ||
and this project tries to adhere to | ||
[Semantic Versioning (SemVer)](https://semver.org/spec/v2.0.0.html). | ||
@@ -17,7 +19,26 @@ <!-- | ||
## 0.3.0 - 2024-12-09 | ||
### Added | ||
- ([PR #11](https://github.com/binyamin/eleventy-plugin-backlinks/pull/11)) | ||
Support 11ty v3 | ||
### Deprecated | ||
- ([PR #11](https://github.com/binyamin/eleventy-plugin-backlinks/pull/11)) Drop | ||
support for 11ty v1 | ||
### Fixed | ||
- ([PR #7](https://github.com/binyamin/eleventy-plugin-backlinks/pull/7)) | ||
Preserve existing global computed data. | ||
## 0.2.1 - 2022-10-26 | ||
### Fixed | ||
- ([`ad2aeff`](https://github.com/binyamin/eleventy-plugin-backlinks/commit/ad2aeffb8d1d5c00f74ec5e757ed8bc56a23ba19)) Normalize strings when identifying backlinks. See [binyamin/eleventy-garden#91](https://github.com/binyamin/eleventy-garden/pull/91). | ||
- ([`ad2aeff`](https://github.com/binyamin/eleventy-plugin-backlinks/commit/ad2aeffb8d1d5c00f74ec5e757ed8bc56a23ba19)) | ||
Normalize strings when identifying backlinks. See | ||
[binyamin/eleventy-garden#91](https://github.com/binyamin/eleventy-garden/pull/91). | ||
@@ -27,4 +48,5 @@ ## 0.2.0 - 2022-06-19 | ||
### Added | ||
- ([`cb21a1f`](https://github.com/binyamin/eleventy-plugin-backlinks/commit/cb21a1f3d1e737e572c6f0c90d63092e05df98af)) Support URLs with `.html` and `.htm` file extensions. | ||
- ([`cb21a1f`](https://github.com/binyamin/eleventy-plugin-backlinks/commit/cb21a1f3d1e737e572c6f0c90d63092e05df98af)) | ||
Support URLs with `.html` and `.htm` file extensions. | ||
@@ -34,2 +56,4 @@ ## 0.1.0 - 2022-06-19 | ||
### Added | ||
- ([`32eeefe`](https://github.com/binyamin/eleventy-plugin-backlinks/commit/32eeefe75e3a6901219c5234e4fade794ac0376c)) Compute backlinks from page data | ||
- ([`32eeefe`](https://github.com/binyamin/eleventy-plugin-backlinks/commit/32eeefe75e3a6901219c5234e4fade794ac0376c)) | ||
Compute backlinks from page data |
57
index.js
@@ -1,6 +0,5 @@ | ||
const backlinks = require("./lib/get-backlinks.js"); | ||
const getExistingData = require("./lib/get-existing-data.js"); | ||
const backlinks = require('./lib/get-backlinks.js'); | ||
const getExistingData = require('./lib/get-existing-data.js'); | ||
/** | ||
* | ||
* @param eleventy | ||
@@ -12,36 +11,36 @@ * @param {object} [options] | ||
*/ | ||
function BacklinksPlugin(eleventy, options={}) { | ||
options = { | ||
folder: 'notes', | ||
getData(note) { | ||
return { | ||
url: note.url, | ||
title: note.data.title, | ||
} | ||
}, | ||
...options, | ||
}; | ||
function BacklinksPlugin(eleventy, options = {}) { | ||
options = { | ||
folder: 'notes', | ||
getData(note) { | ||
return { | ||
url: note.url, | ||
title: note.data.title, | ||
}; | ||
}, | ||
...options, | ||
}; | ||
// Remove leading or trailing slash from `options.folder` | ||
options.folder = options.folder.replace(/^\/?(.+)\/?$/, '$1'); | ||
options.folder = options.folder.replace(/^\/?(.+)\/?$/, '$1'); | ||
function isNoteFile(url) { | ||
return url.startsWith(`/${options.folder}/`); | ||
} | ||
function isNoteFile(url) { | ||
return url.startsWith(`/${options.folder}/`); | ||
} | ||
eleventy.addCollection("notes", function(collection) { | ||
return collection.getAll().filter(t => isNoteFile(t.filePathStem)); | ||
}) | ||
eleventy.addCollection('notes', function (collection) { | ||
return collection.getAll().filter((t) => isNoteFile(t.filePathStem)); | ||
}); | ||
eleventy.addGlobalData("eleventyComputed", { | ||
// Don't overwrite any existing global computed data | ||
...getExistingData(eleventy.globalData), | ||
backlinks(data) { | ||
if (isNoteFile(data.page.filePathStem) === false) return; | ||
eleventy.addGlobalData('eleventyComputed', { | ||
// Don't overwrite any existing global computed data | ||
...getExistingData(eleventy.globalData).eleventyComputed, | ||
backlinks(data) { | ||
if (isNoteFile(data.page.filePathStem) === false) return; | ||
return backlinks(options)(data); | ||
} | ||
}) | ||
return backlinks(options)(data); | ||
}, | ||
}); | ||
} | ||
module.exports = BacklinksPlugin; |
function caselessCompare(a, b) { | ||
return a.normalize().toLowerCase() === b.normalize().toLowerCase(); | ||
return a.normalize().toLowerCase() === b.normalize().toLowerCase(); | ||
} | ||
// This regex finds all wikilinks in a string | ||
const wikilinkRegExp = /\[\[\s*([^\[\]\|\n\r]+)(\|[^\[\]\|\n\r]+)?\s*\]\]/g | ||
const wikilinkRegExp = /\[\[\s*([^\[\]\|\n\r]+)(\|[^\[\]\|\n\r]+)?\s*\]\]/g; | ||
module.exports = function (options) { | ||
return ({ collections, page }) => { | ||
const { notes } = collections; | ||
return async ({ collections, page }) => { | ||
const { notes } = collections; | ||
const fileStem = page.filePathStem.replace(`/${options.folder}/`, ''); | ||
const fileStem = page.filePathStem.replace(`/${options.folder}/`, ''); | ||
let backlinks = []; | ||
let backlinks = []; | ||
// Search the other notes for backlinks | ||
for (const otherNote of notes) { | ||
const noteContent = otherNote.template.frontMatter.content; | ||
// Search the other notes for backlinks | ||
for (const otherNote of notes) { | ||
const noteContent = (await otherNote.template.read()).content; | ||
// Get all links from otherNote | ||
const outboundLinks = (noteContent.match(wikilinkRegExp) || []) | ||
.map(link => ( | ||
// Extract link location | ||
link.slice(2, -2) | ||
.split("|")[0] | ||
.replace(/\.(md|mkd|markdown|html|htm)\s*$/i, "") | ||
.replace(`/${options.folder}/`, '') | ||
.trim() | ||
)); | ||
// Get all links from otherNote | ||
const outboundLinks = (noteContent.match(wikilinkRegExp) || []).map( | ||
(link) => | ||
// Extract link location | ||
link | ||
.slice(2, -2) | ||
.split('|')[0] | ||
.replace(/\.(md|mkd|markdown|html|htm)\s*$/i, '') | ||
.replace(`/${options.folder}/`, '') | ||
.trim(), | ||
); | ||
// If the other note links here, return related info | ||
if (outboundLinks.some(link => caselessCompare(link, fileStem))) { | ||
backlinks.push(options.getData(otherNote)); | ||
} | ||
} | ||
// If the other note links here, return related info | ||
if (outboundLinks.some((link) => caselessCompare(link, fileStem))) { | ||
backlinks.push(options.getData(otherNote)); | ||
} | ||
} | ||
return backlinks; | ||
} | ||
} | ||
return backlinks; | ||
}; | ||
}; |
module.exports = (data) => { | ||
const computed = Object.fromEntries( | ||
Object.entries(data) | ||
.filter(e => e[0].startsWith('eleventyComputed')) | ||
.map(e => [e[0].replace('eleventyComputed.', ''), e[1]]) | ||
); | ||
const computed = Object.fromEntries( | ||
Object.entries(data) | ||
.filter((e) => e[0].startsWith('eleventyComputed')) | ||
.map((e) => [e[0].replace('eleventyComputed.', ''), e[1]]), | ||
); | ||
if ('eleventyComputed' in computed) { | ||
const entry = Object.entries(computed.eleventyComputed); | ||
for (const e of entry) { | ||
computed[e[0]] = computed[[e[1]]]; | ||
} | ||
} | ||
if ('eleventyComputed' in computed) { | ||
const entry = Object.entries(computed.eleventyComputed); | ||
for (const e of entry) { | ||
computed[e[0]] = computed[[e[1]]]; | ||
} | ||
} | ||
return computed; | ||
} | ||
return computed; | ||
}; |
{ | ||
"name": "eleventy-plugin-backlinks", | ||
"version": "0.2.1", | ||
"description": "Collect and display backlinks from your notes", | ||
"keywords": [ | ||
"name": "eleventy-plugin-backlinks", | ||
"version": "0.3.0", | ||
"description": "Collect and display backlinks from your notes", | ||
"keywords": [ | ||
"eleventy", | ||
@@ -14,24 +14,29 @@ "eleventy-plugin", | ||
], | ||
"author": "Binyamin Aron Green (https://binyam.in/)", | ||
"license": "MIT", | ||
"main": "index.js", | ||
"author": "Binyamin Aron Green (https://binyam.in/)", | ||
"license": "MIT", | ||
"main": "index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/binyamin/eleventy-plugin-backlinks.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/binyamin/eleventy-plugin-backlinks/issues" | ||
}, | ||
"homepage": "https://github.com/binyamin/eleventy-plugin-backlinks#readme", | ||
"scripts": { | ||
"test": "node test/index.js" | ||
}, | ||
"devDependencies": { | ||
"@11ty/eleventy": "^1.0.1", | ||
"markdown-it": "^13.0.1", | ||
"markdown-it-wikilinks": "^1.2.0" | ||
}, | ||
"peerDependencies": { | ||
"@11ty/eleventy": ">=1.0.0" | ||
} | ||
"type": "git", | ||
"url": "git+https://github.com/binyamin/eleventy-plugin-backlinks.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/binyamin/eleventy-plugin-backlinks/issues" | ||
}, | ||
"homepage": "https://github.com/binyamin/eleventy-plugin-backlinks#readme", | ||
"scripts": { | ||
"test": "node test/index.js", | ||
"lint": "biome check", | ||
"fmt": "biome fmt" | ||
}, | ||
"dependencies": { | ||
"markdown-it-wikilinks": "^1.4.0" | ||
}, | ||
"devDependencies": { | ||
"@11ty/eleventy": "^2.0.1", | ||
"@biomejs/biome": "1.9.4", | ||
"markdown-it": "^14.1.0" | ||
}, | ||
"peerDependencies": { | ||
"@11ty/eleventy": ">=2.0.0" | ||
} | ||
} |
# Eleventy-Plugin Backlinks | ||
Collect and display backlinks from your notes. | ||
@@ -6,30 +7,44 @@ | ||
> Note: I'm not building this project for myself, so y'all gotta tell me what you think. Issues are the way to go here. | ||
> Note: I'm not building this project for myself, so y'all gotta tell me what | ||
> you think. Issues are the way to go here. | ||
## Usage | ||
## Usage | ||
Add the plugin to your eleventy config, like so: | ||
```js | ||
const eleventyBacklinks = require("eleventy-plugin-backlinks"); | ||
module.exports = function(eleventyConfig) { | ||
eleventyConfig.addPlugin(eleventyBacklinks, { | ||
folder: '/notes' // The folder with your notes | ||
}); | ||
} | ||
module.exports = function (eleventyConfig) { | ||
eleventyConfig.addPlugin(eleventyBacklinks, { | ||
folder: "/notes", // The folder with your notes | ||
}); | ||
}; | ||
``` | ||
For every page in the given folder, you get a `backlinks` variable. By default, it contains an array, with the title (if set) and the url of each page. | ||
For every page in the given folder, you get a `backlinks` variable. By default, | ||
it contains an array, with the title (if set) and the url of each page. | ||
### Options | ||
- **folder** (`string`) - The name of a folder which contains your notes. The default folder-name is "notes". | ||
- **getData** (`function`) - Optionally, control which data will be passed to `backlinks`. The function receives a [collection item](https://www.11ty.dev/docs/collections/#collection-item-data-structure), and is called for every relevant page. | ||
- **folder** (`string`) - The name of a folder which contains your notes. The | ||
default folder-name is "notes". | ||
- **getData** (`function`) - Optionally, control which data will be passed to | ||
`backlinks`. The function receives a | ||
[collection item](https://www.11ty.dev/docs/collections/#collection-item-data-structure), | ||
and is called for every relevant page. | ||
## Contributing | ||
All input is welcome; feel free to [open an issue](https://github.com/binyamin/eleventy-plugin-backlinks/issues/new). Please remember to be a [mensch](https://www.merriam-webster.com/dictionary/mensch). If you want to program, you can browse [the issue list](https://github.com/binyamin/eleventy-plugin-backlinks/issues). | ||
All input is welcome; feel free to | ||
[open an issue](https://github.com/binyamin/eleventy-plugin-backlinks/issues/new). | ||
Please remember to be a | ||
[mensch](https://www.merriam-webster.com/dictionary/mensch). If you want to | ||
program, you can browse | ||
[the issue list](https://github.com/binyamin/eleventy-plugin-backlinks/issues). | ||
## Legal | ||
All source-code is provided under the terms of [the MIT license](https://github.com/binyamin/eleventy-plugin-backlinks/blob/main/LICENSE). Copyright 2022 Binyamin Aron Green. | ||
All source-code is provided under the terms of | ||
[the MIT license](https://github.com/binyamin/eleventy-plugin-backlinks/blob/main/LICENSE). | ||
Copyright 2024 Binyamin Aron Green. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
10012
18
156
50
2
+ Addedmarkdown-it-wikilinks@^1.4.0
+ Addedextend@3.0.2(transitive)
+ Addedmarkdown-it-regexp@0.4.0(transitive)
+ Addedmarkdown-it-wikilinks@1.4.0(transitive)
+ Addedsanitize-filename@1.6.3(transitive)
+ Addedtruncate-utf8-bytes@1.0.2(transitive)
+ Addedutf8-byte-length@1.0.5(transitive)