eleventy-plugin-markdown-page-links 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/changelog.md ADDED
@@ -0,0 +1,10 @@
1
+ # Changelog
2
+
3
+ v0.0.1 - January 11, 2026
4
+
5
+ Initial release.
6
+
7
+ Missing:
8
+
9
+ + Support for External Links only configuration option.
10
+ + Link de-duplication; omitting duplicate links from shortcode output.
@@ -0,0 +1,109 @@
1
+ var ListType;
2
+ (function (ListType) {
3
+ ListType[ListType["Simple"] = 0] = "Simple";
4
+ ListType[ListType["Ordered"] = 1] = "Ordered";
5
+ ListType[ListType["Unordered"] = 2] = "Unordered";
6
+ })(ListType || (ListType = {}));
7
+ const defaultConfig = {
8
+ 'debugMode': false,
9
+ 'collapsible': false,
10
+ 'externalLinksOnly': false,
11
+ 'listClass': '',
12
+ 'listType': 0,
13
+ 'minimumLinks': 0,
14
+ 'openInNewTab': true,
15
+ 'sectionTitle': 'Links',
16
+ };
17
+ const PLUGIN_NAME = 'Eleventy-Plugin-Markdown-Page-Links';
18
+ const regex = /\!*\[([^\]]+)\]\(([^)]+)\)/g;
19
+ function buildLinkList(links, delimiter, newWindow, debugMode) {
20
+ if (debugMode)
21
+ console.log(`Building link list with delimiter: ${delimiter}, newWindow: ${newWindow}`);
22
+ var resultStr = '';
23
+ links.forEach((link) => {
24
+ if (debugMode)
25
+ console.dir(link);
26
+ resultStr += `<${delimiter}><a href="${link.url}"${newWindow ? ' target="_blank" rel="noopener noreferrer"' : ''}>${link.title}</a></${delimiter}>\n`;
27
+ });
28
+ return resultStr;
29
+ }
30
+ export default function (eleventyConfig, options = {}) {
31
+ options = { ...defaultConfig, ...options };
32
+ if (options.debugMode) {
33
+ console.log('Options');
34
+ console.table(options);
35
+ }
36
+ eleventyConfig.addShortcode("pageLinks", function (listType, minimumLinks, openInNewTab, externalLinksOnly, listClass, collapsible, sectionTitle, debugMode) {
37
+ var content;
38
+ var link;
39
+ var links = [];
40
+ var match;
41
+ var resultStr;
42
+ listType = listType ?? options.listType;
43
+ minimumLinks = minimumLinks ?? options.minimumLinks;
44
+ openInNewTab = openInNewTab ?? options.openInNewTab;
45
+ externalLinksOnly = externalLinksOnly ?? options.externalLinksOnly;
46
+ listClass = listClass ?? options.listClass;
47
+ collapsible = collapsible ?? options.collapsible;
48
+ sectionTitle = sectionTitle ?? options.sectionTitle;
49
+ debugMode = debugMode ?? options.debugMode;
50
+ if (listType < 0 || listType > 2) {
51
+ return `${PLUGIN_NAME} Error: Invalid list type specified (${listType}).`;
52
+ }
53
+ content = this.page.rawInput;
54
+ while ((match = regex.exec(content)) !== null) {
55
+ link = {
56
+ title: match[1].trim(),
57
+ url: match[2].trim()
58
+ };
59
+ if (externalLinksOnly && (link.url.startsWith('/') || link.url.startsWith('#') || link.url.startsWith(this.page.url))) {
60
+ if (debugMode)
61
+ console.log(`${PLUGIN_NAME} Skipping internal link: ${link.url}`);
62
+ continue;
63
+ }
64
+ if (links.findIndex(l => l.url === link.url) !== -1) {
65
+ if (debugMode)
66
+ console.log(`${PLUGIN_NAME} Duplicate link found, skipping: ${link.url}`);
67
+ continue;
68
+ }
69
+ links.push(link);
70
+ }
71
+ if (debugMode && links.length > 0)
72
+ console.dir(links);
73
+ resultStr = '';
74
+ if (links.length >= minimumLinks) {
75
+ if (collapsible) {
76
+ resultStr += `<details>\n<summary>${sectionTitle}</summary>\n`;
77
+ }
78
+ switch (listType) {
79
+ case ListType.Ordered:
80
+ if (debugMode)
81
+ console.log(`${PLUGIN_NAME} Building ordered list`);
82
+ resultStr += `<ol${listClass ? ` class="${listClass}"` : ''}>\n`;
83
+ resultStr += buildLinkList(links, 'li', openInNewTab, debugMode);
84
+ resultStr += '</ol>\n';
85
+ break;
86
+ case ListType.Unordered:
87
+ if (debugMode)
88
+ console.log(`${PLUGIN_NAME} Building unordered list`);
89
+ resultStr += `<ul${listClass ? ` class="${listClass}"` : ''}>\n`;
90
+ resultStr += buildLinkList(links, 'li', openInNewTab, debugMode);
91
+ resultStr += '</ul>\n';
92
+ break;
93
+ default:
94
+ if (debugMode)
95
+ console.log(`${PLUGIN_NAME} Building div link list`);
96
+ resultStr += `<div${listClass ? ` class="${listClass}"` : ''}>\n`;
97
+ resultStr += buildLinkList(links, 'p', openInNewTab, debugMode);
98
+ resultStr += '</div>\n';
99
+ break;
100
+ }
101
+ if (collapsible) {
102
+ resultStr += `</details>\n`;
103
+ }
104
+ }
105
+ if (debugMode)
106
+ console.dir(resultStr);
107
+ return resultStr;
108
+ });
109
+ }
Binary file
Binary file
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "eleventy-plugin-markdown-page-links",
3
+ "version": "0.0.5",
4
+ "description": "An Eleventy plugin to generate a list of links for a particular page.",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/johnwargo/eleventy-plugin-markdown-page-links.git"
8
+ },
9
+ "keywords": [
10
+ "eleventy",
11
+ "11ty",
12
+ "plugin"
13
+ ],
14
+ "author": "John M. Wargo",
15
+ "license": "MIT",
16
+ "type": "module",
17
+ "scripts": {
18
+ "build": "tsc && eleventy --quiet",
19
+ "start": "tsc && eleventy --serve --quiet",
20
+ "test": "echo \"Error: no test specified\" && exit 1"
21
+ },
22
+ "bugs": {
23
+ "url": "https://github.com/johnwargo/eleventy-plugin-markdown-page-links/issues"
24
+ },
25
+ "homepage": "https://github.com/johnwargo/eleventy-plugin-markdown-page-links#readme",
26
+ "dependencies": {
27
+ "@11ty/eleventy": "^3.1.2"
28
+ },
29
+ "devDependencies": {
30
+ "@types/node": "^20.4.2",
31
+ "typescript": "^5.3.3"
32
+ }
33
+ }
package/readme.md ADDED
@@ -0,0 +1,155 @@
1
+ # Eleventy Plugin Markdown Post Links
2
+
3
+ ## Tasks
4
+
5
+ - [x] Omit duplicate links in output
6
+ - [x] Implement external-only flag
7
+ - [ ] Update readme
8
+ - [ ] Publish package
9
+ - [ ] Write blog post
10
+ - [ ] Share on social media
11
+
12
+ ---
13
+
14
+ An [Eleventy](https://www.11ty.dev/) plugin that adds a shortcode to Eleventy sites that generates a list of links from a markdown page's content. The plugin supports a variety of configuration options (configurable as global settings and/or individual settings per shortcode use).
15
+
16
+ **Notes:**
17
+
18
+ + Due to some technical limitations in Eleventy, shortcodes don't have access to rendered HTML content for a page. To implement this successfully, the plugin looks for links in pre-rendered content. Since I primarily use markdown files for site content, I built the plugin to look for markdown anchor links.
19
+ + I only tested this plugin using Liquid template code in Markdown files. This approach also makes it easy for the plugin to ignore site navigation on posts, etc.
20
+
21
+ ## Installation
22
+
23
+ To install the plugin, in a terminal window pointing to an Eleventy project, execute the following command:
24
+
25
+ ```shell
26
+ npm install eleventy-plugin-markdown-page-links
27
+ ```
28
+
29
+ In your project's eleventy.config.js file, import the package (at the top of the file) using:
30
+
31
+ ``` ts
32
+ const pageLinks = require('eleventy-plugin-post-stats');
33
+ ```
34
+
35
+ And in the same file's module.exports section, along with all the other plugin statements you site uses, add the following `addPlugin` method call:
36
+
37
+ ``` ts
38
+ module.exports = eleventyConfig => {
39
+
40
+ // add only the following line
41
+ eleventyConfig.addPlugin(pageLinks);
42
+
43
+ }
44
+ ```
45
+
46
+ The complete file should look something like the following (but with your site's other stuff in it as well):
47
+
48
+ ``` ts
49
+ const pageLinks = require('./eleventy-plugin-markdown-page-links.js');
50
+
51
+ module.exports = eleventyConfig => {
52
+
53
+ eleventyConfig.addPlugin(pageLinks);
54
+
55
+ };
56
+ ```
57
+
58
+ With that in place, your site now has a shortcode called `pageLinks` the site can use to generate a list of links from the current page.
59
+
60
+ ## Background
61
+
62
+ Posts in my [personal blog](https://johnwargo.com) often reference a lot of external links, and some of the longer posts have 10 or more links. I realized I could make it easier for readers if I listed all links in a group somewhere on the page. This isn't a critical feature for the site, but something I thought visitors might find useful.
63
+
64
+ Add the shortcode to a page's template to have the links appear on every page, or place the shortcode at the top or bottom of a long article to generate links for just that page. The plugin also supports a configuration option, `minimumLinks`, that instructs the shortcode to generate links only if there are a specific number of links on the page.
65
+
66
+ The plugin supports the following options:
67
+
68
+ **List Types:**
69
+
70
+ + **Simple list** - Generates a simple list of page links with each as a separate paragraph in the page.
71
+ + **Ordered list** - Generates a list of page links as an ordered list using the `<ol>` and `</ol>` tags
72
+ + **Unordered list** - Generates a list of page links as an ordered list using the `<ul>` and `</ul>` tags
73
+
74
+ **List Options:**
75
+
76
+ + **Collapsible list** - Link list and renders it in a collapsed section on the page using the `<details>` and `<summary>` tags.
77
+ + **New Tab** - Links open in a new tab using `target="_blank"`.
78
+ + **Styled List** - Allows you to apply a CSS class to the list container on the page.
79
+ + **Minimum Links** - Only generate the link list when there's at least a specified number of links on the page.
80
+
81
+ This repo includes a [sample app](https://mdpagelinks.netlify.app/) (hosted for free on [Netlify](https://www.netlify.com/)) that demonstrates all of these options; check it out to learn more.
82
+
83
+ ## Operation
84
+
85
+ Add the shortcode `pageLinks` to a page or page template to generate a list of the page's links in that location. In this example, no list type is specified, so the plugin uses the default option of the simple list.
86
+
87
+ ``` markdown
88
+ Links on this page:
89
+
90
+ {% pageLinks %}
91
+
92
+ Bacon ipsum dolor amet picanha filet mignon beef ribs swine tongue kielbasa...
93
+ ```
94
+
95
+ This generates the following content on the page:
96
+
97
+ ![Sample links list](/images/image-01.png)
98
+
99
+ Specify a different list type (such as unordered list: 2) by specifying the list type as a parameter to the shortcode:
100
+
101
+ ``` markdown
102
+ Links on this page:
103
+
104
+ {% pageLinks 2 %}
105
+
106
+ Bacon ipsum dolor amet picanha filet mignon beef ribs swine tongue kielbasa...
107
+ ```
108
+
109
+ This generates the unordered list shown below:
110
+
111
+ ![Sample links list](/images/image-02.png)
112
+
113
+ ### Configuration Options
114
+
115
+ The shortcode supports the following options:
116
+
117
+ | Name | Option | Description |
118
+ | --------------- |-------------------- | ----------- |
119
+ | Collapsible | `collapsible` | Generates a collapsible section containing the list of links. When you enable this option, you must also specify a value for `sectionTitle`. [Example](https://mdpagelinks.netlify.app/posts/collapsible/). Default: `false` |
120
+ | External Links | `externalLinksOnly` | Omits local links from the generated output. [Example](https://mdpagelinks.netlify.app/posts/external-only/). Default: `false` |
121
+ | List Class | `listClass` | Defines the name of the CSS Class you want added to the list container. [Example](https://mdpagelinks.netlify.app/posts/styled/). Default: "" |
122
+ | List Type | `listType` | Specifies the type of list generated; supported options are `0` {Simple), `1` (Ordered), `2` (Unordered). Examples: [Simple](https://mdpagelinks.netlify.app/posts/simple/), [Ordered](https://mdpagelinks.netlify.app/posts/ordered/), [Unordered](https://mdpagelinks.netlify.app/posts/unordered/). Default: 0 |
123
+ | Minimum Links | `minimumLinks` | Specifies the minimum number of links on the page required before the shortcode generates the link list. If your site generally doesn't have a lot of links per page, you can use this it to only generates links for articles with a greater number of links. [Example](https://mdpagelinks.netlify.app/posts/minimum/). Default: 0 |
124
+ | Open in New Tab | `openInNewTab` | By default, the shortcut generates links that open in a new browser window/tab. With this option set to `false`, when a user clicks one of the links, the linked content will replace the current page in the browser. [Example](https://mdpagelinks.netlify.app/posts/same-window/). Default: `true` |
125
+ | Section Title | `sectionTitle` | With `collapsible` enabled, this configuration option defines the text the plugin generates as the Summary (`<summary></summary>`) of the collapsible section. If you don't provide a value for this configuration option, the shortcut uses the default value. [Example](https://mdpagelinks.netlify.app/posts/collapsible/). Default: "Links" |
126
+
127
+ You can set any of these settings as global options for the shortcut when you load the plugin in your project's `eleventy.config.js` file. Simply add any supported configuration variable in an object to the `addPlugin` method invocation. This following example configures the plugin to open all links in the same browser tab/window (disabling the default behavior).
128
+
129
+ ``` ts
130
+ eleventyConfig.addPlugin(pageLinks, { openInNewTab: false });
131
+ ```
132
+
133
+ Specify any of the configuration options as parameters to the shortcode; as positional parameters, the order is the following:
134
+
135
+ 1. `listType?` ListType
136
+ 2. `minimumLinks`: number
137
+ 3. `openInNewTab`: boolean
138
+ 4. `externalLinksOnly`: boolean
139
+ 5. `listClass`: string
140
+ 6. `collapsible`: boolean
141
+ 7. `sectionTitle`: string
142
+
143
+ The `ListType` enum looks like this:
144
+
145
+ ``` ts
146
+ enum ListType {
147
+ Simple, // 0
148
+ Ordered, // 1
149
+ Unordered, // 2
150
+ }
151
+ ```
152
+
153
+ **Note:** Some time after launch, but perhaps before launch, I'll configure the shortCode code to use named parameters.
154
+
155
+ Refer to the [sample app](https://mdpagelinks.netlify.app/) for examples for each of these options.