Last active
May 9, 2025 18:39
-
-
Save scottandrewlepera/0e7715090b1baf77dd0c072d803ad98b to your computer and use it in GitHub Desktop.
feedExclude.js - Exclude content from 11ty feeds
This file contains hidden or 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
/* | |
feedExclude.js - Exclude content from 11ty feeds | |
This filter replaces: | |
- any element with the "feed-exclude" class (recommended) | |
- any content found between <feed-exclude> tags | |
...with a link to the same post on the website. | |
Add as a filter to your 11ty content, then call the filter in your | |
feed template with the URL of the post: | |
feedExclude(absolutePostUrl) | |
- You will need to add jsdom to your devDependencies | |
- Not tested with 11ty virtual templates | |
- Does not support nested <feed-exclude> tags | |
Post content: | |
<p>Here is some content.</p> | |
<p class="feed-exclude">Here is some content to be excluded.</p> | |
<p>Here is some more content.</p> | |
<feed-exclude> | |
<p>Here is even more content to be excluded.</p> | |
<feed-exclude> | |
Feed output: | |
<p>Here is some content.</p> | |
<p>(<a href="https://example.com/url/to/post">Visit the website to see this content.</a>)</p> | |
<p>Here is some more content.</p> | |
<p>(<a href="https://example.com/url/to/post">Visit the website to see this content.</a>)</p> | |
*/ | |
const { JSDOM } = require('jsdom'); | |
module.exports = function(content, url) { | |
const message = `(<a href="${url}">Visit the website to see this content.</a>)`; | |
const dom = new JSDOM(content); | |
const doc = dom.window.document; | |
const elements = doc.querySelectorAll('.feed-exclude, feed-exclude'); | |
elements.forEach(el => { | |
const p = doc.createElement('p'); | |
p.innerHTML = message; | |
el.replaceWith(p); | |
}); | |
return doc.body.innerHTML; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment