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

Is it possible to use a different markdown library based on layout? #1626

Closed
divillysausages opened this issue Feb 5, 2021 · 4 comments
Closed

Comments

@divillysausages
Copy link

In the docs, you can supply your own markdown library instance like so:

let markdownIt = require( "markdown-it" );
let options = {
	typographer: true,
	quotes: '“”‘’',
};
eleventyConfig.setLibrary( "md", markdownIt( options ) );

Is it possible to do something like this, but only for specific layouts? For example, I have a default layout, which keeps straight quotes etc, and a fancy layout which converts to typographic quotes and all that jazz.

As far as I can tell it's for all markdown files or nothing

Thanks

@pdehaan
Copy link
Contributor

pdehaan commented Feb 5, 2021

Pretty sure that isn't possible (and not sure how eleventy would know which renderer to use for markdown). Possible workaround is to just created a paired shortcode which wraps markdown-it and then just toggle between the default layout and a custom typographer layout.

Repo: https://github.com/pdehaan/11ty-md-layout-1626

// .eleventy.js config file
const markdownIt = require( "markdown-it" );

const mdLib = markdownIt( {
  typographer: true,
  quotes: '“”‘’',
  html: true,
} );

module.exports = eleventyConfig => {
  // eleventyConfig.setLibrary( "md", mdLib);

  eleventyConfig.addPairedShortcode("typographer", (content) => mdLib.renderInline(content));

  return {
    dir: {
      input: "src",
      output: "www"
    }
  };
};

Base layout is in ./src/_includes/layouts/base.liquid:

<!DoCtYpE html>
<html lang="en">
<head>
  <meta charset="utf-8"/>
  <title>{{ title }}</title>
</head>
<body>

  <main>
  {{ content }}
  </main>

</body>
</html>

Custom typographer layout extends our base layout and uses the custom {% typographer %} paired shortcode, and is in ./src/_includes/layouts/typographer.liquid:

---
layout: layouts/base.liquid
---

<div class="typographer">
{% typographer %}{{ content }}{% endtypographer %}
</div>

src/pages/two.md can specify the custom "layouts/typographer.liquid" layout from the ./src/_includes/ directory:

---
title: two (custom)
layout: layouts/typographer.liquid
---

<div>

Hello, my name is "Peter".

They're a "nice" thing.

</div>

And finally, the output is:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>two (custom)</title>
  </head>
  <body>
    <main>
      <div class="typographer">
        <div>
          <p>Hello, my name is “Peter”.</p>
          <p>They’re a “nice” thing.</p>
        </div>
      </div>
    </main>
  </body>
</html>

@divillysausages
Copy link
Author

As for how it would work, I had thought of calling setLibrary( "mdTypographic", markdownIt( options )) in .eleventy.js, then setting templateEngineOverride: mdTypographic in the front matter of the file, but setLibrary only seems to work for the defined types.

The paired shortcode is a good idea though; I'll give it a try tomorrow, thanks.

renderInline is how markdown is transformed normally, is that correct?

@pdehaan
Copy link
Contributor

pdehaan commented Feb 5, 2021

renderInline is how markdown is transformed normally, is that correct?

I think internally the Markdown template engine uses markdown-it's .render() method, per

  1. let finishedRender = mdlib.render(preTemplateEngineRender, data);
  2. return mdlib.render(str, data);

But I think the feature you might be requesting is #117.

Typography can potentially lead to some interesting problems though. One thing I guess I never tested for, was if you had quotes around an HTML tag's attributes. You'd want those to be straight quotes and not smart quotes. But in other places within the document where the single/double quotes appear within text blocks, you'd want to use smart quotes instead.

@divillysausages
Copy link
Author

Apologies for not replying a lot sooner, but just to let you know, your paired shortcode did the trick, thanks @pdehaan !

One thing I guess I never tested for, was if you had quotes around an HTML tag's attributes. You'd want those to be straight quotes and not smart quotes.

If you add html in the markdown file along the lines of <div id="someID">...</div> it correctly uses straight quotes. The only issue I've found in my (admittedly limited) testing, is the case of starting quotes for a missing letter (e.g. 'tis, short for it is). In that case it uses a straight quote, but it's a small thing.

Thanks again

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

2 participants