{"id":3491,"date":"2016-03-23T00:27:10","date_gmt":"2016-03-23T00:27:10","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3491"},"modified":"2016-03-23T00:27:10","modified_gmt":"2016-03-23T00:27:10","slug":"templated-markdown-handlebars","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/templated-markdown-handlebars\/","title":{"rendered":"Templated markdown with handlebars"},"content":{"rendered":"<p>To used templated markdown files, we need a library for markdown (showdown) and a templating library (handlebars). A good resource for finding well-maintained libraries is <a href=\"https:\/\/www.javascripting.com\/\">https:\/\/www.javascripting.com\/<\/a>.<\/p>\n<pre lang=\"javascript\">\nlet showdown = require('showdown'),\n    Handlebars = require('handlebars');\n<\/pre>\n<p>We&#8217;ll need some sample data:<\/p>\n<pre lang=\"javascript\">\nlet data = {\n  Title: 'A test title',\n  Description: 'Testing description'\n}\n<\/pre>\n<p>And a template file:<\/p>\n<pre lang=\"javascript\">\nlet template = `\n## Test\nTitle:<br>\nSlug:<br>\nExcerpt:<br><br>\n*[{{Title}}]({{Url}})*:<br><br>\n> {{Description}}<br><br>\n`;\n<\/pre>\n<p>We then apply the handlebars template, and then a markdown template:<\/p>\n<pre lang=\"javascript\">\nlet apply = Handlebars.compile(template)\nlet untemplate = apply(data)\nlet converter = new showdown.Converter();\nlet html = converter.makeHtml(untemplate)\n<\/pre>\n<p>This gives a result like so:<\/p>\n<pre lang=\"html\">\n<h2 id=\"test\">Test<\/h2>\n\n<p>Title:<br>\nSlug:<br>\nExcerpt:<br><br>\n<em><a href=\"\">A test title<\/a><\/em>:<br><br><\/p>\n\n<blockquote>\n  <p>Testing description<br><br><\/p>\n<\/blockquote>\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>To used templated markdown files, we need a library for markdown (showdown) and a templating library (handlebars). A good resource for finding well-maintained libraries is https:\/\/www.javascripting.com\/. let showdown = require(&#8216;showdown&#8217;), Handlebars = require(&#8216;handlebars&#8217;); We&#8217;ll need some sample data: let data = { Title: &#8216;A test title&#8217;, Description: &#8216;Testing description&#8217; } And a template file: let &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/templated-markdown-handlebars\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Templated markdown with handlebars&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[302,358],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3491"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=3491"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3491\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}