DEV Community

mafflerbach
mafflerbach

Posted on

Automate Publishing with Front Matter Variables

Dev.to supports Font Matter. That means you can manipulate the publishing date with variables.
In this Article i am playing around with this variables and evaluate the possibilities for my blogging workflow.

As i already mentioned, we can use a special section which contains variables.
E.g. this Article contains following header:

---
title: Automate Publishing with Front Matter Variables
published: false
series: Publishing workflow
---
Enter fullscreen mode Exit fullscreen mode

I set a title, a series, and prevent from direct publishing. But when we take a closer look on the documentation, we have more possibilities which i want to use in my workflow.

Based on the official Documentation, i can use following Font Matter variables:

title: Hello, World!
published: true
tags: discuss, help
date: 20190701T10:00Z
series: Hello series
canonical_url: https://example.com/blog/hello
cover_image: article_published_cover_image
Enter fullscreen mode Exit fullscreen mode

date, series and canonical_url are optional. date is the publication date-time series is the name of the series the article belongs to canonical_url is the canonical URL of the article cover_image is the main image of the article.

The most interesting variables are date, tags and published. I want to find out the behaviour of date vs published.
I would expect that, if published is true and the date is in the future, the article becomes published on the given date.
But i have to test what happen, if the published variable is false but i am using a date variable.
With usage of variables I get also the possibility to simplify my curl call, but i have to be aware that it contains always a Front Matter block with at least the title and the published variables.

Sadly after testing and playing it seems that the date variable has no influence over the real publishing date.

For this approach i will adapt my script accordingly:

#!/bin/bash
source ~/dotfiles/.credencials

filePath=$1
# Content has to be json encoded
content=$(cat "$filePath" | jq '.' --raw-input --slurp)

# to keep it simple, use the first 6 lines
headOfContent=$(cat "$filePath" | head -n6)

if [[ "$headOfContent" != *"title"* ]]; then
  echo "No title Flag found"
  exit
fi

if [[ "$headOfContent" != *"published"* ]]; then
  echo "No published Flag found"
  exit
fi

tee /tmp/devtoArticle.md << END
{"article":{"body_markdown":$content}}
END

curl -X POST -H "Content-Type: application/json" \
    -H "api-key: ${devtoApiToken}" \
    -d @/tmp/devtoArticle.md \
    https://dev.to/api/articles

#get the current date
date=$(date +%m-%d-%Y)
# grab only the filename
fileName=$(echo "$filePath" | cut -d"/" -f3)
mv "$filePath" "$(pwd)/content/published/$date $fileName"
Enter fullscreen mode Exit fullscreen mode

The call of my script is now really easy:

./helper.sh 'content/drafts/Automate Publishing with Front Matter Variables.md' 
Enter fullscreen mode Exit fullscreen mode

After this changes, the current workflow is really charming. I know this script is by far not bullet proof but it is enough for me.

Top comments (0)