tannerdolby / eleventy-plugin-metagen

Eleventy shortcode that generates document metadata
https://www.npmjs.com/package/eleventy-plugin-metagen
MIT License
41 stars 3 forks source link

Support "description" like "desc" #22

Closed silveltman closed 2 years ago

silveltman commented 2 years ago

I would love to be able to use "description" instead of "desc".

tannerdolby commented 2 years ago

@silveltman Thank you for raising this issue! I will add support for the description argument.

tannerdolby commented 2 years ago

Fixed in https://github.com/tannerdolby/eleventy-plugin-metagen/commit/4149c795a90bc320172679fd860f5eb079fa0920. You can now use description in v1.7.8 🚀

silveltman commented 2 years ago

This actually leads me to 2 other things.

  1. Can you also support "image" on top of "img"

  2. My setup is a data file (seo.yaml) with all tags:

    title:
    description:

My frontmatter per page is:

seo:
  title:
  description:

I want the frontmatter to overwrite the data file. However, if I leave the title or description in the frontmatter empty, the metagen get's these empty values instead of defaulting to the data file. I know this could probably an 11ty thing, but maybe there is a fix within this plugin.

Thanks!

tannerdolby commented 2 years ago

@silveltman I apologize for the delayed response, I'm quite busy and this thread got lost in my stack of "todo replies".

  1. Can you also support "image" on top of "img"? - Yes I can add support for "image" which will by synonymous with the current img field. Expect this to be added in a new version release within the next week.

  2. My setup is a data file (seo.yaml) with all tags .. I want the frontmatter to overwrite the data file. However, if I leave the title or description in the frontmatter empty, the metagen get's these empty values instead of defaulting to the data file.

So for your second question, global data files have the lowest priority in the data cascade where frontmatter in a template has the 2nd highest priority in the cascade.

However, if I leave the title or description in the frontmatter empty, the metagen get's these empty values instead of defaulting to the data file.

The above helped me to better understand your question. If you could share your metagen usage in the templates that would help me to better provide a solution, but in the meantime if I had the following global data file _data/seo.yaml then if I didn't supply title or description as a top-level field in frontmatter, we could tell the plugin to fallback to the global data file something like this:

---
seo:
  title: Foo bar
  description: some description
---

Metagen might not be getting called here in the same template 
(it might be inheriting a layout and thats where the frontmatter is consumed, 
but for the sake of this example I'm just showing the example metagen call here)

Example metagen call falling back to the global data file 
(which can be directly accessed in templates via {{ seo.someField }} 
or without the curly braces inside metagen paramters e.g. title=title or seo.someFIeld.

<head>
    {% metagen 
        title=title or seo.title,
        desc=desc or seo.desc,
       ...
    %}
</head>

Hopefully the above examples can be helpful in some way. In the meantime, feel free to create a more reproducible example (include metagen calls) in a new issue and I'm happy to have a closer look.

silveltman commented 2 years ago

Thanks for the response!

I changed it up a bit, but still can't get it to work.

_data/general_settings.yml:

seo:
  title: Cool title
  description: Cool description
  url: https://mywebsite.com
  img:
  img_alt:
  name:
  locale:
  generator: eleventy

Below my ideal setup in which the metagen gets values from general_settings.seo if not null or empty. So in the example below it would use the title and description from general_settings.seo. I have to leave the keys for title and desciption because of my CMS setup for my clients.

---
seo:
  title:
  description:
---

<head>
    {% metagen general_settings.seo or seo  %}
</head>

Something like the example you gave would also be fine, less clean I think. Also, I'm getting an error this way. Below the setup I tried after reading your example:

---
seo:
  title:
  description:
---

<head>
    {% metagen 
       title=seo.title or general_settings.seo.title 
   %}
</head>

this gives me the following error:

Problem writing Eleventy templates: (more in DEBUG output)
[11ty] 1. Having trouble writing template: "_site/components/index.html" (via EleventyTemplateError)
[11ty] 2. invalid syntax at line 1 col 6:
[11ty]
[11ty]   title=title or seo.title
[11ty]        ^, file:C:\Users\silve\websites\11ty\starters\11ty-cloudcannon-starter\src\_includes\head.liquid, line:37, col:1 (via RenderError)
[11ty] 3. invalid syntax at line 1 col 6:
[11ty]
[11ty]   title=title or seo.title
[11ty]        ^ (via Error)
[11ty]
[11ty] Original error stack trace: Error: invalid syntax at line 1 col 6:
[11ty]
[11ty]   title=title or seo.title
[11ty]        ^
[11ty]     at Lexer._token (C:\Users\silve\websites\11ty\starters\11ty-cloudcannon-starter\node_modules\moo\moo.js:533:13)
[11ty]     at Lexer.next (C:\Users\silve\websites\11ty\starters\11ty-cloudcannon-starter\node_modules\moo\moo.js:480:19)
[11ty]     at Function.parseArguments (C:\Users\silve\websites\11ty\starters\11ty-cloudcannon-starter\node_modules\@11ty\eleventy\src\Engines\Liquid.js:121:21)       
[11ty]     at async Object.render (C:\Users\silve\websites\11ty\starters\11ty-cloudcannon-starter\node_modules\@11ty\eleventy\src\Engines\Liquid.js:145:26)
[11ty]     at async TemplateLayout.render (C:\Users\silve\websites\11ty\starters\11ty-cloudcannon-starter\node_modules\@11ty\eleventy\src\TemplateLayout.js:161:25)   
[11ty]     at async Template.renderPageEntry (C:\Users\silve\websites\11ty\starters\11ty-cloudcannon-starter\node_modules\@11ty\eleventy\src\Template.js:853:17)      
[11ty]     at async C:\Users\silve\websites\11ty\starters\11ty-cloudcannon-starter\node_modules\@11ty\eleventy\src\Template.js:883:21
[11ty]     at async Promise.all (index 0)
[11ty]     at async Promise.all (index 1)
[11ty]     at async Eleventy.executeBuild (C:\Users\silve\websites\11ty\starters\11ty-cloudcannon-starter\node_modules\@11ty\eleventy\src\Eleventy.js:1003:13)        
[11ty] Wrote 0 files in 0.23 seconds (v1.0.1)

all examples are with the same _data/general_settings.yml file

UPDATE: same error when calling the metagen with something like this:

{% metagen
  title="testings"
%}

or this?

{% metagen
  title: "testings"
%}
tannerdolby commented 2 years ago

Something like the example you gave would also be fine, less clean I think. Also, I'm getting an error this way. Below the setup I tried after reading your example:

Thank you for the detailed summary. I much better understand your situation which makes helping you easier 😄

The .liquid usage is different than for Nunjucks files.njk and this the cause for the:

11ty] Original error stack trace: Error: invalid syntax at line 1 col 6:
[11ty]
[11ty]   title=title or seo.title

The metagen plugin is primarily unit tested on .njk templates which the name=value syntax for shortcode parameters is expected. You could convert head.liquid to head.njk and re-run your trials which should work as expected, that is if no other .liquid files are relying on the Nunjucks shortcode syntax. The name: value syntax is not allowed in shortcode arguments in either Nunjucks or Liquid so that is out of the picture.

If the above doesn't help get you on the right path, I see that you have the code in a GitHub repo. If its public feel free to share the repo URL and I'll have a closer look to propose a solution for your scenario.

When using {% metagen title="testings" %} within a .njk template, the shortcode returns the expected output without any build-breaking errors:

Screen Shot 2022-06-20 at 11 52 44 AM
tannerdolby commented 2 years ago

Also, I just published v1.7.9 which supports data.image and anywhere img is used will be synonymous with image.

silveltman commented 2 years ago

Thanks for all the help! Tho, I think sharing my github repo is a good idea hahah

https://github.com/silveltman/11ty-cloudcannon-starter

I got it to work in the page debug.liquid, but when using it in _includes/metagen.njk (which is then used in head.njk) I get the same 'invalid syntax' error as before.