Skip to content

Instantly share code, notes, and snippets.

@PaulEibensteiner
Last active September 5, 2018 03:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PaulEibensteiner/c2f1d107deef3a0d7d30fa25ef0a198e to your computer and use it in GitHub Desktop.
Save PaulEibensteiner/c2f1d107deef3a0d7d30fa25ef0a198e to your computer and use it in GitHub Desktop.
Custom Editor widget for netlify cms to add shortcode
<!-- Bottom Part of the index.html inside amdmin directory. The UI element is in spanish because of the prject I'm working on right now. Also If you use the id "image"you apparently overwrite the default image widget which you can or cannot want -->
<script>
CMS.registerEditorComponent({
// Internal id of the component
id: "imagen",
// Visible label
label: "Añadir Imagen",
// Fields the user need to fill out when adding an instance of the component
fields: [{name: 'id', label: 'Lugar del Imagen', widget: 'image'}],
// Pattern to identify a block as being an instance of this component
pattern: /{{< image name="(.+)" >}}/,
// Function to extract data elements from the regexp match
fromBlock: function(match) {
return {
id: match[1]
};
},
// Preview output for this component. Can either be a string or a React component
// (component gives better render performance)
toPreview: function(obj) {
return (
'<img src="' + '/images/' + obj.id + '" alt="image"/>'
);
},
// Function to create a text block from an instance of this component
toBlock: function(obj) {
return '{{< image name="' + obj.id.substring(8) + '" >}}';
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment