{"id":10236,"date":"2023-07-26T11:29:17","date_gmt":"2023-07-26T09:29:17","guid":{"rendered":"https:\/\/www.main-vision.com\/richard\/blog\/?p=10236"},"modified":"2023-07-26T11:29:34","modified_gmt":"2023-07-26T09:29:34","slug":"yq-frontmatter-and-jekyll","status":"publish","type":"post","link":"https:\/\/www.main-vision.com\/richard\/blog\/yq-frontmatter-and-jekyll\/","title":{"rendered":"YQ, FrontMatter and Jekyll"},"content":{"rendered":"<span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\"> 4<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span>\n<p class=\"wp-block-paragraph\">For two days I have been trying to understand how to use Jekyll, a Ruby version of Hugo. It is a Static website generator that is similar to Hugo but rather than being written in Go, it is written in Ruby. I find that it renders sites faster than Hugo, but that it has less assistance for creating pages, giving them layouts and the rest. That&#8217;s why I experimented with <a href=\"https:\/\/github.com\/mikefarah\/yq\">YQ<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">a lightweight and portable command-line YAML, JSON and XML processor. <a href=\"https:\/\/github.com\/mikefarah\/yq\">YQ<\/a> uses jq like syntax but works with yaml files as well as json, xml, properties, csv and tsv. It doesn&#8217;t yet support everything jq does &#8211; but it does support the most common operations and functions, and more is being added continuously.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I read about it in <a href=\"https:\/\/roneo.org\/en\/hugo-edit-yaml-files-from-the-cli-with-yq\/\">this article<\/a>. You can install <a href=\"https:\/\/github.com\/mikefarah\/yq\">YQ<\/a> with a brew command brew install yq or you can install it in a number of other ways, depending on whether you&#8217;re using MacOS, Linux or Windows.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The Use case<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Exporting a WordPress blog to MD files can be done with a multitude of tools but they don&#8217;t add all the FrontMatter that you need for a blog, especially Jekyll. I imported the most recent blog posts, that I created for Hugo, with a year-month-date-title.md name to the posts folder but I created a second one for the archive. As the file title was different Jekyll did not like the files. The problem was the file name format, so that&#8217;s why I created a separate directory.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Side Note on Reading the Archives MD Files<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/g.co\/bard\/share\/995044bd6897\">Bard&#8217;s help<\/a>: I provide this to help you understand Jekyll logic, as well as Bard help.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Getting It To Work On MacOS<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the article about using YQ to edit FrontMatter for Hugo pages the first part worked fine, but it&#8217;s written for Linux rather than macOS so some commands failed. This is where Bard comes in.You can ask Bard to explain what parts of a line of code or a command does and it will explain it to you, and you can eventually transfer the command that is understood by one OS and get it to work with the other.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I tried this find -name &#8220;*.md&#8221; -exec yq &#8211;front-matter=&#8221;process&#8221; &#8216;.updated_at = now&#8217; {} \\; but it failed to work. <a href=\"https:\/\/g.co\/bard\/share\/5ef10215c9d5\">Bard was kind enough to explain the options<\/a> for me to figure out which options to use. I added the correct path, and specified that I was looking for the files with the &#8216;type f&#8217; flag.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">find .\/_posts -type f -name &#8220;*.md&#8221; -exec yq &#8216;.title&#8217; {} \\; To see the title of .md files&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I wanted to add these two lines to the FrontMatter of the files in the relevant folder.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I tested find ..\/unprocessed &#8220;*.md&#8221; -exec yq &#8211;front-matter=&#8221;process&#8221; &#8216;.updated_at = now&#8217; {} \\; and this works but does not make changes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This works and updates the pages to add the updated at field to the metadata with the keyword now. The -i tells it to write to the file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The full command is find ..\/unprocessed -name &#8220;*.md&#8221; -exec yq e &#8211;front-matter=&#8221;process&#8221; &#8216;.updated_at = now&#8217; -i {} \\; works to add updated now tag .<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Implementing the Required Change<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The lines that I wanted to add to the front matter were &#8216;layout: post&#8217; and &#8216;categories: [archives]&#8217;. After the trial and error described above I tried:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">find ..\/unprocessed -name &#8220;*.md&#8221; -exec yq e &#8211;front-matter=&#8221;process&#8221; &#8216;.updated_at = now&#8217; -i {} \\; works to add updated now tag<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">find ..\/unprocessed -name &#8220;*.md&#8221; -exec yq e &#8211;front-matter=&#8221;process&#8221; &#8216;.layout = post&#8217; -i {} \\; works to add updated now tag<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Finally the command to make a permanent change was:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">find ..\/_archives -name &#8220;*.md&#8221; -exec yq e &#8211;front-matter=&#8221;process&#8221; &#8216;.layout = &#8220;post&#8221;&#8216; -i {} \\;&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">find ..\/_archives -name &#8220;*.md&#8221; -exec yq e &#8211;front-matter=&#8221;process&#8221; &#8216;.categories = &#8220;[categories]&#8221;&#8216; -i {} \\;&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A Quick explanation. The ..\/archives needs to be replaced with the name of the folder your files are in, when terminal i in the same folder. .layout and .categories are the names of the field to add and the &#8220;text&#8221; part is the content that you want to add to the front matter.<\/li>\n\n\n\n<li>I have unprocessed and archives folders because I tested the code on two files to start with, and when I saw that it worked, then I moved to the main archives folder. I also used git to have a backup of the latest versions. If something had gone wrong I could have &#8220;stashed&#8221; the mistake, and been back to normal.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The first line ads the layout information to the FrontMatter post and the second one adds the categories information to the posts. Instead of spending hours doing something manually I was able to find the right tools to do what I wanted within seconds.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The Process<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I knew what I wanted to do. I started with a google search to see if I could find a tool with some instructions and I did. The instructions were useful, but of limited use for my setup so I used the app&#8217;s documentation to try one thing, establishing that this worked, before moving on to accomplish the task that I had set for myself-. Google Bard was able to provide me with some help, but so did chatGPT.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The value of AI tools, in my eyes, is to help us understand the code we&#8217;re looking at, but also to help us tweak it for the OS we&#8217;re using, when we get error messages. I appreciate that with AI we can ask &#8220;I&#8217;m getting this error message, why?&#8221; and it will help find an answer to the question.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If a web article had provided me with a solution that worked I would have stuck to the web page and I would have little to write about. It is because of trial and error, and using four sources to achieve the goal that I wanted to achieve, that it becomes blog worthy.<\/p>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\"> 4<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span>For two days I have been trying to understand how to use Jekyll, a Ruby version of Hugo. It is a Static website generator that is similar to Hugo but rather than being written in Go, it is written in Ruby. I find that it renders sites faster than Hugo, but that it has less [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":10237,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":3,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[255],"tags":[5268,5197,5298,5296],"class_list":["post-10236","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-swiss-walks","tag-frontmatter","tag-hugo","tag-jekyll","tag-yq"],"jetpack_publicize_connections":[],"_links":{"self":[{"href":"https:\/\/www.main-vision.com\/richard\/blog\/wp-json\/wp\/v2\/posts\/10236","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.main-vision.com\/richard\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.main-vision.com\/richard\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.main-vision.com\/richard\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.main-vision.com\/richard\/blog\/wp-json\/wp\/v2\/comments?post=10236"}],"version-history":[{"count":1,"href":"https:\/\/www.main-vision.com\/richard\/blog\/wp-json\/wp\/v2\/posts\/10236\/revisions"}],"predecessor-version":[{"id":10238,"href":"https:\/\/www.main-vision.com\/richard\/blog\/wp-json\/wp\/v2\/posts\/10236\/revisions\/10238"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.main-vision.com\/richard\/blog\/wp-json\/wp\/v2\/media\/10237"}],"wp:attachment":[{"href":"https:\/\/www.main-vision.com\/richard\/blog\/wp-json\/wp\/v2\/media?parent=10236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.main-vision.com\/richard\/blog\/wp-json\/wp\/v2\/categories?post=10236"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.main-vision.com\/richard\/blog\/wp-json\/wp\/v2\/tags?post=10236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}