Skip to content
This repository has been archived by the owner on May 5, 2021. It is now read-only.

Declare material ID in partial front-matter #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

skinread
Copy link
Contributor

As alluded to in fbrctr/fabricator#127 we were looking for a way to refer to partials by an ID.

Initially this was parsed from the filename but with the latest updates to allow for numbered ordering, it became unfeasible to delimit the ID in filename itself.

This solution uses an optional declaration in the material file and creates a second handlebars registration with the ID if present.

example usage


---
materialId: C7
notes:

---

The partial can then be called via {{> C7}} or it can still be called by the file name as usual.

I've also moved the name concatenation out of the conditional block since the ID is output in the material title when available.

…t register the partial as well as display the id in the name output
@LukeAskew
Copy link
Member

Looks good.

I think you can still find the ID using the bracket [ ] syntax defined in your original issue.

Is this the expected behavior: ?

[C7]-component-name.html -> {{> C7}}
00-[C7]-component-name.html -> {{> C7}}
folder/[C7]-component-name.html -> {{> C7}}

Just messed around a little, and I think changing the getName() function is a good place to start...something like:

var getName = function (filePath, preserveNumbers) {

    // match text inside [brackets]
    var idRegex = /\[(.+?)\]/;

    // match leading numbers
    var numRegex = /^[0-9|\.\-]+/;

    // extract filename from path
    var name = path.basename(filePath, path.extname(filePath));

    // if filename contains bracket delimiters, return text inside brackets
    if (name.match(idRegex)) {
        return name.match(idRegex)[1];
    }

    // otherwise, return name with/without numbers preserved
    return (preserveNumbers) ? name : name.replace(numRegex, '');

};

I think I am fine with using frontmatter, but I like the bracket syntax a little better.

@skinread
Copy link
Contributor Author

Hi, thanks for much for taking the time to provide feedback!

I've been away for a few weeks so I haven't been able to look at this again until now.
The expected behaviour you list above is what we had intended, however at the time I tried to get it working with the latest code, it was becoming over complex to have the file names parsed consistently with the numbered ordering AND the [ ] delimiting.

We have been using the ID definition in the front matter at it has reduced the maintance we have to carry out managing the file names. If you still prefer to have both options available (file name or front matter to declare the id) I will try to incorporate both options as suggested.

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

Successfully merging this pull request may close these issues.

2 participants