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

Enhancement: setting variables for contentTypes fields in template defintion #225

Closed
wemasoe opened this issue Jan 14, 2022 · 20 comments
Closed
Labels
enhancement New feature or request
Projects
Milestone

Comments

@wemasoe
Copy link

wemasoe commented Jan 14, 2022

First at all, a really nice project. I appreciate using this extension.

For now i don´t have understand how to use variables in the frontmatter conent-type definition.
When creating a new content the title: and slug: are filled with the name of the filename.
The article.md template file has two variable definitions.
Example:

title: "{{name}}"
slug: "/{{kebabCase name}}/"

Is it somehow possible to define this variable for other taxonomy content types.

Example:

title: "{{name}}"
slug: "/{{kebabCase name}}/"
permalink: "{{name}}"

For now only the title: and slug: has the filename filled out.

title: created filename
slug: created-filename
permalink: '{{name}}'

How can i resolve this?

@estruyf
Copy link
Owner

estruyf commented Jan 14, 2022

@wemasoe thanks for opening this issue. These placeholders are a leftover from when the extension didn't support content types.

At the moment only the title and slug are automatically propagated on the creation. In the upcoming version, you can also define default values for your content type fields.

Placeholder support is indeed a good one to get added as well.

@estruyf estruyf added the enhancement New feature or request label Jan 14, 2022
@wemasoe
Copy link
Author

wemasoe commented Jan 14, 2022

@estruyf thx for the reply.

Placeholder support is indeed a good one to get added as well.

this would be very nice 👍

@estruyf
Copy link
Owner

estruyf commented Jan 14, 2022

Feel free to suggest some placeholders you'd like to use.

@wemasoe
Copy link
Author

wemasoe commented Jan 14, 2022

placeholders you'd like to use.

well, i am just diving a little into the code.
i thnk it would be nice when generating the template to reference to a helper variable?
Maybe there are other methods solving this?

in this particular need i would like to use a permalink: like "filename.html"

There are already constants like in ArticleHelper.ts:

export const SETTING_TAXONOMY_CONTENT_TYPES = "taxonomy.contentTypes";

export interface Field {
  title?: string;
  name: string;
  type: "string" | "number" | "datetime" | "boolean" | "image" | "choice" | "tags" | "categories" | "draft" | "taxonomy";
  choices?: string[] | Choice[];
  single?: boolean;
  multiple?: boolean;
  isPreviewImage?: boolean;
  hidden?: boolean;
  taxonomyId?: string;
}

there is also already a taxonomy type

TaxonomyType.ts
export enum TaxonomyType {
  Tag = 1,
  Category
}

suggestion1:

taxonomy_helper_varname1: $var_prefix
taxonomy_helper_varname2: "{{title}}"  (or anything else defined in template fields: "slug", "title", "date", "or taxonomy" ,"fieldname")
taxonomy_helper_varname3: $var_suffix
taxonomy_helper_varname_concatenate: taxonomy_helper_varname[0-2]
title: created filename
slug: created-filename
permalink: '{{taxonomy_helper_varname_concatenate}}'
type: documentation

or maybe like this?

suggestion2:

taxonomy_helper_varname: "{{title}}"  (or anything else defined in template fields: "slug", "title", "date", "or taxonomy" ,"fieldname")
title: created filename
slug: created-filename
permalink: '{{taxonomy_helper_varname}}'
type: documentation

https://frontmatter.codes/docs/content-types#define-your-own-type

"frontMatter.taxonomy.contentTypes": [
  {
    "name": "default",
    "fields": [
      ...
    ]
  },
  {
    "name": "documentation",
    "fields": [
      ...
    ]
  }
]

ContentType is defined as follow in the documentation section fields:
frontmatter.json

		{
          "title": "Permalink",
          "name": "permalink",
          "type": "string"
        },

well now i am on my limit :-)

"frontMatter.taxonomy.customTaxonomy": [
  {
    "id": "permalink",
    "options": [
      "prefix",
      "taxonomy_helper_varname(read from template definition field (taxonomy_helper_varname))",
      "suffix",
    ]
  }
]

or

        {
          "title": "Permalink",
          "name": "permalink",
          "type": "choice",
          "choices": ["taxonomy_helper_varname",".html", ".json", ".js", "prefix"]
        },

prefix could be null.
suffix could also be null (".html", ".json", ".js")

i am not sure what would be most effective.

@estruyf
Copy link
Owner

estruyf commented Jan 14, 2022

There are already constants like in ArticleHelper.ts:

These are the extension setting constants in order to understand how your environment is configured.

there is also already a taxonomy type

Taxonomy is a list of predefined data like tags/categories/custom-data to use in your front matter article content. Not really used for defining the helpers, or at least I don't see how we could use it for that.

Think I'm not really following with the logic. Just to reclarify for my mind. You want to be able to define placeholders that will be updated once contents get created. For instance, a unique ID or GUID can be one of the placeholders to include.

Others could be:

  • {{title}}: Returns the title of the page
  • {{slug}}: Returns a sanitized title of the page concatenated with -'s
  • {{now}}: Returns the current time string
  • ...

Your example of supporting a permalink would require extra logic. As you want to include a prefix and suffix. I think it might complicate it a bit, as there are various ways to configure the extension.

Think a new setting like frontMatter.content.placeholders would be more appropriate, but instead of defining options/fields, what if you'd define a script that the extension executes? You get the file path, front matter data, ... All you need to do is create the value to insert into the file. A bit like we do on the custom actions/scripts - https://frontmatter.codes/docs/custom-actions

@estruyf
Copy link
Owner

estruyf commented Jan 14, 2022

Or frontMatter.content.placeholders could be as follows:

{
	"frontMatter.content.placeholders": [
		{
			"id": "permalink",
			"value": "<prefix>-{{slug}}<suffix>"
		}
	]
}

<prefix> and <suffix> will need to be added by you. For example:

{
	"frontMatter.content.placeholders": [
		{
			"id": "permalink",
			"value": "/docs/{{slug}}.html"
		}
	]
}

In your template or content type, you define the value of the field to: {{permalink}}.

---
permalink: {{permalink}}
---

This results in a front matter data value as:

---
permalink: /blog/placeholder-documentation.html
---

@wemasoe
Copy link
Author

wemasoe commented Jan 14, 2022

{
	"frontMatter.content.placeholders": [
		{
			"id": "permalink",
			"value": "<prefix>-{{slug}}<suffix>"
		}
	]
}

yes :-) that looks like a fine solution :-)

Like you already mentioned.

Others could be:

{{title}}: Returns the title of the page
{{slug}}: Returns a sanitized title of the page concatenated with -'s
{{now}}: Returns the current time string
...

Your answer also clarified the script section you mentioned, for me.

@estruyf: thx a lot for your detailed answers

@estruyf estruyf added this to In progress in v6.0.0 Jan 15, 2022
estruyf added a commit that referenced this issue Jan 15, 2022
@estruyf estruyf moved this from In progress to Ready to test in v6.0.0 Jan 15, 2022
@estruyf estruyf added this to the 6.0.0 milestone Jan 15, 2022
@estruyf
Copy link
Owner

estruyf commented Jan 15, 2022

The first implementation of this enhancement has been added to the beta.

Currently supported placeholders are: {{title}} and {{slug}}. If you want to add custom placeholders, you can add this in your settings or frontmatter.json file as follows:

"frontMatter.content.placeholders": [
    {
      "id": "permalink",
      "value": "/blog/{{slug}}.html"
    }
  ]

The known placeholders from Front Matter itself can also be used in the custom ones.

@wemasoe
Copy link
Author

wemasoe commented Jan 16, 2022

@estruyf thank you for the enhancement implementation. This realy helps me a lot.

When using the new frontmatter setting it works when using templates. Like the Article.md

"frontMatter.content.placeholders": [
    {
      "id": "permalink",
      "value": "/blog/{{slug}}.html"
    }
  ]

How do i define it in my own contenttype settings?
With this configuration i don´t get the placeholder been rewritten.

"frontMatter.taxonomy.contentTypes": [
  {
    "name": "default",
    "fields": [
      ...
    ]
  },
  {
    "name": "documentation",
    "fields": [
      ...
        {
          "title": "Permalink",
          "name": "permalink",
          "type": "string"
        },
      ...
    ]
  }
]

Output:

---
title: newfile
permalink: ''
description: ''
date: '2022-01-16T11:51:53.791Z'
preview: ''
draft: ''
tags: ''
categories: ''
weight: ''
test: ''
sidebar: ''
type: documentation
---

"optimize slug" can not be selected without a slug: definition in the frontmatter of the generated file.

@estruyf
Copy link
Owner

estruyf commented Jan 16, 2022

Content type

  • in your content type, you can now use the default property and set it to {{permalink}} in your case.
{
  "title": "Permalink",
  "name": "permalink",
  "type": "string",
  "default": "{{permalink}}"
}

"optimize slug" can not be selected without a slug: definition in the frontmatter of the generated file.

  • This is another enhancement, it might require a content/front matter scan in order to enable/disable it.

@wemasoe
Copy link
Author

wemasoe commented Jan 18, 2022

  • enhancement, it might require a content/front matter scan in order to enable/disable it.

@estruyf yes that would be fine

the placeholder is not beeing set, when using choice.

...
        {
          "title": "Permalink1",
          "name": "permalink1",
          "type": "choice",
          "choices": [
            "{{permalink}}",
            "{{title}}"
          ]
        },
...
  "frontMatter.content.placeholders": [
    {
      "id": "permalink",
      "value": "{{title}}.html"
    }
  ],
...

@estruyf
Copy link
Owner

estruyf commented Jan 18, 2022

Choice fields and taxonomy fields cannot be used like this. They can only be used to select a predefined value.

Can you try to explain your use case or how you want to use it?

@wemasoe wemasoe closed this as completed Jan 18, 2022
@wemasoe
Copy link
Author

wemasoe commented Jan 18, 2022

Choice fields and taxonomy fields cannot be used like this. They can only be used to select a predefined value.

Can you try to explain your use case or how you want to use it?

i just played around and tried some things out to check how dynamic the placeholder and variables are in the dashboard.

@estruyf
Copy link
Owner

estruyf commented Jan 19, 2022

Ok, that makes it clear! Thanks for the feedback 🙏

@estruyf
Copy link
Owner

estruyf commented Jan 19, 2022

The optimize slug button, now updates all fields that had a default value set to the {{slug}} placeholder + any custom placeholders that depend on {{slug}} as well.

@wemasoe
Copy link
Author

wemasoe commented Jan 19, 2022

The optimize slug button, now updates all fields that had a default value set to the {{slug}} placeholder + any custom placeholders that depend on {{slug}} as well.

that sounds good. But can you give an example?
i have tried this?
that is how i understood the feature?

        {
          "title": "Slugtest",
          "name": "slugtest",
          "type": "string",
          "default": "{{slug}}"
        },
  "frontMatter.content.placeholders": [
    {
      "id": "slug",
      "value": "{{slug}}.html"
    }
  ],

@estruyf
Copy link
Owner

estruyf commented Jan 20, 2022

No, it works differently. {{slug}} is a known placeholder from Front Matter, what you try to do is overwrite it and will not work.

What will work, is if you go back to the permalink approach:

{
  "title": "Slugtest",
  "name": "slugtest",
  "type": "string",
  "default": "{{permalink}}"
}
"frontMatter.content.placeholders": [
  {
    "id": "permalink",
    "value": "{{slug}}.html"
  }
],

@wemasoe
Copy link
Author

wemasoe commented Jan 20, 2022

image

it did not show any action.

        {
          "title": "Permalink",
          "name": "permalink",
          "type": "string",
          "default": "{{permalink}}"
        },
  "frontMatter.content.placeholders": [
    {
      "id": "permalink",
      "value": "{{slug}}.html"
    }
  ],

@estruyf
Copy link
Owner

estruyf commented Jan 20, 2022

Latest applied fix will resolve this issue.

@wemasoe
Copy link
Author

wemasoe commented Jan 20, 2022

@estruyf thx :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
No open projects
Development

No branches or pull requests

2 participants