Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access to raw file content (before any processing) #1206

Closed
sjml opened this issue May 22, 2020 · 20 comments
Closed

Access to raw file content (before any processing) #1206

sjml opened this issue May 22, 2020 · 20 comments

Comments

@sjml
Copy link

sjml commented May 22, 2020

I know from the page object I can access content and within a collection I can use templateContent to get the rendered output, but is it possible to get just the markdown input?

I could just read the file from the page's inputPath, but then I also get the frontmatter... it's easy enough to make a quick shortcode to return what I'm after, but I wanted to make sure there wasn't something built-in that I was missing...

@sjml sjml added the education label May 22, 2020
@MiloCasagrande
Copy link

I think frontMatter.content holds the markdown, without the frontmatter part.

@nhoizey
Copy link
Contributor

nhoizey commented May 25, 2020

@MiloCasagrande I didn't see that anywhere, would you have an example?

@MiloCasagrande
Copy link

@nhoizey If you meant an example in the docs, I didn't see any in there.
This is what gives me the markdwon from within a collection. I assume that whatever the looping mechanism the same data is available:

---
pagination:
  data: collections.post
  size: 10
  alias: posts
---
{% for post in posts %}
  {{ post.template.frontMatter.content | log }}
{% endfor %}

@nhoizey
Copy link
Contributor

nhoizey commented May 26, 2020

Thanks, it works in loops indeed! 👍

But I can't make it work in a content's template. Any idea how to access it?
I tried page.template.frontMatter.content and multiple others…

Also, I guess it should be documented, if we want it to keep working in the future… 😅

@MiloCasagrande
Copy link

@nhoizey unfortunately not, I was only able to access it from within the pagination loop.

Those data structures should be documented somewhere, I also fear that they might disappear in the future.

@nhoizey
Copy link
Contributor

nhoizey commented May 26, 2020

Ok.

@zachleat can we expect this to stay in the future? Also, is there a way to access the same value in a content's template/layout?

@aarongustafson
Copy link
Contributor

I agree it would be useful to access the raw Markdown for instances where you might wat to port/copy content to another system that speaks Markdown, but not HTML.

@zachleat zachleat changed the title Access to raw markdown for a page? Access to raw file content (before any processing) Oct 28, 2020
@zachleat
Copy link
Member

Another vote for this here https://twitter.com/rob_dodson/status/1321554550671618048 from @robdodson

@robdodson
Copy link

yep, here's how we use this in web.dev to build our algolia index
https://github.com/GoogleChrome/web.dev/blob/master/src/site/_data/lib/algolia.js#L62

but I feel a little nervous using an undocumented API 😅

@aarongustafson
Copy link
Contributor

Interesting, thanks for sharing @robdodson. We are about to use the following in a custom collection for https://github.com/webwewant/webwewant.fyi:

  eleventyConfig.addCollection("wantsBySubmissionDate", collection => {
    return collection.getAll()
             .filter( item => item.inputPath.match(/\/wants\/.*\.md/) !== null )
             .sort( (a, b) => b.date - a.date )
             // append the raw content
             .map( item => {
               item.data.rawMarkdown = item.template.frontMatter.content || "";
               return item;
             } );
  });

Note that in the map() we’re appending it to the item’s data object as rawMarkdown.

@nhoizey
Copy link
Contributor

nhoizey commented Oct 28, 2020

@robdodson I didn't think about using the raw Markdown as a source for Algolia after cleaning, well done. I'm currently cleaning the rendered HTML with cheerio. I guess using remove-markdown would be much faster.

I'm also using item.template.frontMatter.content in a collection, to extract hashtags as tags: https://github.com/nhoizey/nicolas-hoizey.com/blob/master/src/_11ty/collections/notes.js#L12

I guess having the raw Markdown everytime, in any collection, would be useful.

@zachleat zachleat added the needs-votes A feature request on the backlog that needs upvotes or downvotes. Remove this label when resolved. label Feb 25, 2021
@zachleat
Copy link
Member

This repository is now using lodash style issue management for enhancements. This means enhancement issues will now be closed instead of leaving them open.

View the enhancement backlog here. Don’t forget to upvote the top comment with 👍!

@mtsknn
Copy link

mtsknn commented Jul 31, 2021

My hacky way of achieving this:

// .eleventy.js
config.setLibrary('md', { render: (markdown) => markdown })

Now Markdown files don't get automatically converted to HTML, so content properties (or templateContent; see #882) are raw Markdown. In layouts, I can manually convert Markdown to HTML by calling markdown-it's render method.

A downside is that if a page is rendered multiple times (e.g. once on its own and once when rendering an RSS feed), markdown-it's render method is called multiple times. I tried to "cache" the value like this:

// eleventyComputed.js
module.exports = {
  html: (data) => markdownIt.render(data.content),
}

...but it doesn't work because content/templateContent is not available in computed properties (see #971).

@zachleat
Copy link
Member

@boehs
Copy link

boehs commented Feb 8, 2023

Cross link: https://fosstodon.org/@eleventy/109831381492142746

I use it to extract wikilinks, and it's not working in 2.0! 😱

I'm probably just going to stick on 2.0.0-canary.20 for now, there might be a newer working version, but /shrug

Issue from this file:

"Unfortunately you’re using code that monkey patched some Eleventy internals and it isn’t async-friendly."

@zachleat zachleat pinned this issue Mar 20, 2023
zachleat added a commit that referenced this issue Dec 16, 2023
@zachleat zachleat removed the needs-votes A feature request on the backlog that needs upvotes or downvotes. Remove this label when resolved. label Dec 16, 2023
@zachleat
Copy link
Member

zachleat commented Dec 16, 2023

Shipping with Eleventy v3.0.0-alpha.1 as the rawInput property on Eleventy programmatic API calls (e.g. toJSON) and collection items.

zachleat added a commit that referenced this issue Dec 16, 2023
@zachleat zachleat unpinned this issue Dec 16, 2023
@zachleat zachleat pinned this issue Dec 16, 2023
@zachleat zachleat unpinned this issue Dec 16, 2023
@zachleat
Copy link
Member

zachleat commented Jan 10, 2024

I think we’ll add page.rawInput too in v3.0.0-alpha.5! Stay tuned!

(see the rest of page here: https://www.11ty.dev/docs/data-eleventy-supplied/#page-variable)

@edwardhorsford
Copy link
Contributor

edwardhorsford commented Jan 12, 2024

Will it be possible to read rawInput before the other page variables like content?

I've got a collection I want to make that is derived based on the content of existing pages. Pre-1.0 I read through the templateContent of each post looking for a string match. 1.0 and later, I get a tried to read templateContent too early error. My solution is to use the page.filePath with fs.readFileSync to manually read the files to search their contents.

@zachleat
Copy link
Member

zachleat commented Feb 9, 2024

@edwardhorsford yeah, I think so—it should be available long before content in the order of operations

zachleat added a commit that referenced this issue Feb 9, 2024
@zachleat zachleat added the needs-documentation Documentation for this issue/feature is pending! label May 30, 2024
zachleat added a commit to 11ty/11ty-website that referenced this issue May 30, 2024
@zachleat
Copy link
Member

Docs for page.rawInput shipping to https://www.11ty.dev/docs/data-eleventy-supplied/#page-variable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants