I’d like to make some custom widgets for Netlify CMS. I’m using webpack/eleventy.
The problem I’m having is, once I save the entry or switch from Rich Text/Markdown the widget UI stops working.
This is a dummy example. Let’s say I have two fields and I want to output some HTML from it.
CMS.registerEditorComponent({
id: "header",
label: "header",
fields: [
{ name: 'heading', label: 'heading', widget: 'string' },
{ name: 'text', label: 'text', widget: 'string' }
],
// This is a dummy regex pattern
pattern: /^.*>$/,
// I need to return the matching fields from the toBlock function.
fromBlock: function (match) {
return {
heading: match[1],
text: match[2]
};
},
// What renders on the block editor
toBlock: function (obj) {
return `<h1>${obj.heading}</h1>\n<p>${obj.text}</p>`;
},
// What renders on the preview window
toPreview: function (obj) {
return `<h1>${obj.heading}</h1>\n<p>${obj.text}</p>`;
}
});
The regex pattern is not always going to work for more complex widgets with 3-5 different elements, this could be text or images.
Hi @brandonjhz and welcome to the community Sorry for the late reply.
Since editor components are meant to be able to parse raw markdown files the pattern should be able to extract the fields from the raw content file.
There is a bit more complex example with 3 fields here:
@brandonjhz and @solomonpierce, can you share an example that isn’t working? e.g. share a content of a markdown file, the block that should be generated and parsed and the fields that you configure
From what I understand toBlock is what is added to the actual post but I figured it would be similar to the image editor component by posting what is in toPreview
I have tried copying what is in toBlock to toPreview and made adjustments to the regex pattern to capture it but that did not work either. Is there something I am missing here?
Hi there! Thanks for your interest in Netlify CMS. Looks like you posted your question a little while ago, but that you haven’t received a solution yet. Here’s where you might get more help:
netlifycms.org - the site houses our extensive documentation that likely contains helpful information to get you back on track.
netlify cms slack - join our friendly slack channel and chat with other cms pros to get the help you need.
GitHub Issues - think you’ve found a bug, or would like to make a feature request? Make your voice heard here. Netlify CMS is open source - PRs and other contributions are also welcome!
Stack Overflow Check StackOverflow for questions tagged “Netlify CMS” if you don’t get an answer in the Slack or the GH issues. StackOverflow reaches a worldwide audience of knowledgeable people.
Your question will be left open here for anyone to comment - but we encourage you to check out the above resources if you are still looking for a solution!