Add template content to computed data #4032
Replies: 20 comments
|
Got something strange when I |
|
Might be better using |
|
Here's what I would like to be able to do. I have a Markdown file which content includes some ---
date: 2020-01-01 00:00:00 +01:00
tags: [POSSE, Twitter, Mastodon]
---
I'm starting sharing my quick thoughts on my own site instead of Twitter, and then #POSSE them on #Twitter, #Mastodon and others, following [repeated advice from Tantek Çelik](https://tantek.com/2020/001/t1/10-years-notes-my-site).Instead of duplicating these hashtags into the YFM Here's what I could put in the directory data file: const twitter = require('twitter-text');
module.exports = {
layout: "note",
permalink: "/notes/{{ page.date | notePermalinkDate }}-{{ page.fileSlug }}/",
eleventyComputed: {
tags: data => {
if (data.content === undefined) {
return data.tags || [];
}
let tags = twitter.extractHashtags(twitter.htmlEscape(data.content));
if (data.tags !== undefined) {
// merge and deduplicate
tags = [...new Set([].concat(...tags, ...data.tags))]
}
return tags;
}
}
};This currently doesn't do what I want because |
|
Anything on this one? I am having the exact same problem: every function in "eleventyComputed" is called twice; via a console.dir() I can see these loads of (((())))) as you described earlier, and then the second time I have a clean object with everything I need. First: And then: |
|
Unfortunately Computed Data requires two runs to establish correct order of execution with dependencies. I tried real hard to use proxies for this but some template languages (Nunjucks is one) visited every key in the data object so unfortunately it wasn’t feasible there :( Might be able to do some additional optimizations on this later to avoid double runs in some cases. |
|
Also going to have to put this one into the enhancement queue, unfortunately. It would not be a quick task to reorder the cascade to support this.
|
|
For now, I'm using this and it seems to be working nicely |
|
@TigersWay looks like it works indeed. Would you be able to explain where this test on |
|
😅 It was - during beta-2 - the quick way to know "data" was something interesting.... It's all working now. But in fact it's worse now as "eleventyComputed" seems to have the highest priority wich lead me to I am right now working on a "complex" and hierachical set of documents. |
|
Ok, so now you say it is computed first, so we keep the value if it's already there. Right? |
|
It seems to be. To be honest, I am a little bit out of reach trying to understand that part of Eleventy code. "Mon coté français, peut-être"...... |
😅 I'll try like you did. However, my own need is to compute tags from content. It works for current content (I can list tags of a note without having a static But I still don't know how to get these tags in the global tags collection… |
|
We are somehow leaving the main topic of this issue 😷 I would try something like what I did with my buildNavigation (https://github.com/TigersWay/11ty-netlify-playground/blob/master/_11ty/filters/navigation.js), add a collection which would be just meant to modify the main one. |
|
@TigersWay I was leaving the main topic indeed… 😅 I like how you compute branches and leaves! 👍 Back to the main topic, I managed to add tags from the content to the tags Front Matter: But I still didn't find the way to list these contents in the collection of the tag. It might need another issue, through. |
I'm working.. slowly these days ... on a plugin about this exactly: eleventy-plugin-ancestry |
|
If you want to get the raw content of the file being processed, you can read it in a JS data file: // _content/posts/posts.11tydata.js
const fs = require('fs');
module.exports = {
eleventyComputed: {
someData: async data => {
const filePath = data.page.inputPath.replace("./_content/posts", __dirname);
const fileContent = await fs.readFileSync(filePath, 'utf8');
return fileContent;
}
}
}This did not seem to have any impact on processing speed with 108 small files but all the usual caveats about I/O bottlenecks apply. |
|
I now use the template content to get hashtags in the collection creation directly. Look for I could copy the full template content in the collection item, but I only need the hashtags in this site. |
|
Cross-link to #3458 (comment) (request specific to Eleventy Layouts) |
Uh oh!
There was an error while loading. Please reload this page.
I love the new Computed Data feature! 👍
However, it would be even more awesome if it could also use
templateContent, at least as a data source.My use case is the following:
I would like to write some of my content just like tweets, with #hashtags, and extract them with a filter in computed data to generate the
tagslist for the content.All reactions