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

Allow any frontmatter property to be overridden by site.json #784

Merged
merged 2 commits into from
Apr 14, 2019

Conversation

jamos-tay
Copy link
Contributor

@jamos-tay jamos-tay commented Mar 20, 2019

What is the purpose of this pull request? (put "X" next to an item, remove the rest)

• [X] Enhancement to an existing feature

Fixes #647

What is the rationale for this request?

We want to be able to globally override all frontmatter properties in a page

What changes did you make? (Give an overview)

Allow any property in the frontmatter to be overriden by globalOverride if specified in the site.json,

{
  "globalOverride": {
      "arbitraryProperty": "..."
  }
}

Proposed commit message:

Allow any frontmatter property to be overridden by site.json

Some front matter properties can be overridden by site.json, but not 
all.

Let's allow any front matter property to be overriden by specifying 
them at the site.json level. Additionally, let's allow the user to 
define global front matter properties that will be applied to all 
pages.

yamgent
yamgent previously approved these changes Mar 21, 2019
Copy link
Member

@yamgent yamgent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Propose a merge commit message.

@jamos-tay
Copy link
Contributor Author

My bad, I can repush with a better message

"Allow global override of frontmatter properties"?

@yamgent
Copy link
Member

yamgent commented Mar 23, 2019

My apologies, maybe I wasn't clear, but when I mean a merge commit message, it is something like this.

Basically it is a new format that we are adopting to explain the context of the change. The first paragraph is usually the current situation, the second paragraph explains why the current situation needs changes, and the third paragraph introduces our changes.

If you are strapped for time, just tell me here and I will try to come up one for you.

@jamos-tay
Copy link
Contributor Author

Sure, no problem

Global override for frontmatter properties #784

Currently only certain properties such as `layout` in the frontmatter can be overridden by the site.json, but not properties such as `footer`.

We would like to allow frontmatter properties on every page to be globally overridden to give the author more flexibility. This also allows us to arbitrarily add properties to every page.

We introduce a `globalOverride` property in the site.json which is merged with every page's frontmatter, and overrides any similar properties.

@jamos-tay
Copy link
Contributor Author

Rebased and updated

@yamgent
Copy link
Member

yamgent commented Mar 23, 2019

Thanks for the commit message! I tidy up a bit to suit our convention better:

Add global override for frontmatter properties (#784)

Certain properties such as `layout` in the frontmatter can be overridden
by the site.json, but not properties such as `footer`. Overriding
properties also apply to only a single `page` or `glob` entry.

We would like to allow all frontmatter properties on every page to be 
globally overridden to give the author more flexibility. By providing
it as a  global option, this also allows us to add the same set of
properties and their values to every page.

Let's introduce a `globalOverride` property in the site.json which is 
merged with every page's frontmatter, and overrides any similar 
properties.

Rough description on changes made:

  • Message should be wrapped at 72 characters.
  • Title should start with imperative verbs like "Add".
  • Deleted "Currently" for the first paragraph - it is implied.
  • Added "Overriding properties also apply to only a single pageorglob entry." and rephrased the rest accordingly.
  • Change "We" to "Let's" for the last paragraph (this is where the commit comes in and "takes action").

On a side note,

Current: some frontmatter properties can be overridden by site.json, but not all.

Suggestion: allow any frontmatter property to be overridden by site.json

See discussion in #506

I don't think the original post expected it to be globally overridden, but rather that the individual page and glob entries can also override the properties. I know the subsequent posts did cover about global overrides, but unless I missed some discussion about that, I feel that the globals are a nice complement feature but not the full solution, so the spirit of the original post isn't really completely fulfilled by this PR?

@damithc
Copy link
Contributor

damithc commented Mar 23, 2019

I don't think the original post expected it to be globally overridden, but rather that the individual page and glob entries can also override the properties. I know the subsequent posts did cover about global overrides, but unless I missed some discussion about that, I feel that the globals are a nice complement feature but not the full solution, so the spirit of the original post isn't really completely fulfilled by this PR?

yes, that's correct. The original expectation is to be able to override at src/glob level, not globally.

@jamos-tay
Copy link
Contributor Author

So we want something like this?

    {
      "glob": "**/index.md"
      "someProperty": ...
    }

@damithc
Copy link
Contributor

damithc commented Mar 23, 2019

So we want something like this?

    {
      "glob": "**/index.md"
      "someProperty": ...
    }

Yes 👍

@jamos-tay jamos-tay changed the title Global override any front matter properties with site.json [WIP] Global override any front matter properties with site.json Mar 26, 2019
@jamos-tay jamos-tay changed the title [WIP] Global override any front matter properties with site.json Global override any front matter properties with site.json Mar 26, 2019
@jamos-tay
Copy link
Contributor Author

Updated

User can now specify a frontmatter property in pages:

    {
      "src": "index.md",
      "title": "Hello World",
      "frontmatter": {
        "property": "val"
      }
    },

globalOverride will override pages, pages will override frontmatter

@damithc
Copy link
Contributor

damithc commented Mar 26, 2019

User can now specify a frontmatter property in pages:

Nice. Ensure that if a property is specified multiple times (e.g., via multiple globs) the latest value takes precedence, based on the order given in the site.json.

globalOverride will override pages, pages will override frontmatter

An alternative is to rename this as defaultFrontmatter and let them be overridden by values specified at page level (but in site.json). That way, the user can specify the typical values as defaults and override only where some pages needs to differ. But in any case, site.json should override frontmatter in individual files, to comply with our top-down overriding policy. What do you think?

Copy link
Contributor

@nicholaschuayunzhi nicholaschuayunzhi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the pr @jamos-tay! I've left some comments regarding the overriding of values. Perhaps we could make it more explicit we are using default attributes if front matter doesn't exists. Additionally, the site.json override could be a separate step that affects both branches. What do you think?

src/Page.js Outdated
src: this.src,
title: this.title || '',
layout: LAYOUT_DEFAULT_NAME,
...{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why this is in an object?
Perhaps we could do

const defaultAttributes = {
        src: this.src,
        title: this.title || '',
        layout: LAYOUT_DEFAULT_NAME,
}

this.frontMatter = {
    ...defaultAttributes 

Additionally, this could collapse with the logic above? the title check and layout check seems redundant there.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought perhaps we could do something like

let front matter = { default }
if (specified front matter exists)
    front matter = specified front matter

// handle overrides

Copy link
Contributor Author

@jamos-tay jamos-tay Mar 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm this is a bit tricky because the default src and title actually takes priority over the specified front matter, we only use the specified one if they are undefined. But we also can't overwrite specified front matter with default because overriding with ... seems to include undefineds:

a = { x: 5 }
b = { x: undefined }
{...a, ...b } // { x: undefined }

layout: LAYOUT_DEFAULT_NAME,
},
...this.frontmatterOverride,
...this.globalOverride,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here value specified by site.json, namely this.title would be overriden by frontmatterOverride and globalOverride if they specified a title. Would this be a concern?

Copy link
Contributor Author

@jamos-tay jamos-tay Mar 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not, it was discussed here that it's easier to keep it consistent than specially handle it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah i see, then line 515 and 517 should be done before 512? Otherwise it would override frontmatterOverride and globalOverride which is inconsistent when front matter is not specified.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, makes sense to move it down for consistency

@jamos-tay
Copy link
Contributor Author

User can now specify a frontmatter property in pages:

Nice. Ensure that if a property is specified multiple times (e.g., via multiple globs) the latest value takes precedence, based on the order given in the site.json.

globalOverride will override pages, pages will override frontmatter

An alternative is to rename this as defaultFrontmatter and let them be overridden by values specified at page level (but in site.json). That way, the user can specify the typical values as defaults and override only where some pages needs to differ. But in any case, site.json should override frontmatter in individual files, to comply with our top-down overriding policy. What do you think?

Sure, makes sense. It's not too much trouble to swap the priority

@jamos-tay
Copy link
Contributor Author

Sorry for the late update, updated and rebased

@@ -152,6 +159,10 @@ The following properties will apply to `index.md`:

**An array of external scripts to be referenced on all pages.** To reference an external script only on specific pages, `externalScripts` should be specified in `pages` instead. Scripts referenced will be run before the layout script.

#### **`globalOverride`**

**Globally overrides properties in the front matter of all pages.** Any property included in the global override will automatically be merged with the front matter of every single page, and override them if the property exists. Also overrides front matter properties specified in `pages`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This documented behaviour is no longer true? frontMatter now has a higher overriding priority than globalOverride with the latest implementation.

Also update frontMatter's description accordingly.

@yamgent yamgent added this to the v2.1.2 milestone Apr 13, 2019
@yamgent yamgent changed the title Global override any front matter properties with site.json Allow any frontmatter property to be overridden by site.json Apr 14, 2019
@yamgent yamgent merged commit c254e7b into MarkBind:master Apr 14, 2019
@yamgent yamgent mentioned this pull request Apr 14, 2019
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow any frontmatter property to be overridden by site.json
5 participants