Markdown Meta Action
Content containing some YAML frontmatter is very common nowadays (in fact, you're reading a post that uses it right now).
Today's action allows you to use that data in any way that you need in your workflows.
| Fact Sheet | |
|---|---|
| Author | mheap |
| Contributors | 1 |
| Stars | 0 |
| Repo | https://github.com/mheap/markdown-meta-action |
| Marketplace | https://github.com/marketplace/actions/markdown-meta |
What does it do?
markdown-meta allows you to read that metadata and makes it available as an output in your GitHub Actions workflow.
The frontmatter for this file is as follows:
yaml---title: Markdown Meta Actiondescription: Read the frontmatter from your markdown files and use the values in your workflowsslug: markdown-meta-actiondate: "2021-05-07T19:01:58+01:00"category: "Tech"tags:- github-actions---
Given that file we can run the following workflow that outputs the post title:
yamlname: Get Post Metaon: pushjobs:post-meta:name: Post Metaruns-on: ubuntu-lateststeps:- name: Checkoutuses: actions/checkout@v2- name: Markdown Metauses: mheap/markdown-meta-action@v1id: metawith:file: ./_posts/markdown-meta-action.md- name: Output the post titlerun: echo "${{ steps.meta.outputs.title }}"
We use the id parameter to name the markdown-meta step meta and then access the data returned by it using steps.meta.outputs. Each key in the frontmatter is available:
steps.meta.outputs.titlesteps.meta.outputs.descriptionsteps.meta.outputs.slugsteps.meta.outputs.datesteps.meta.outputs.categorysteps.meta.outputs.tags
How does it work?
This is a really tiny action, just 7 lines long:
- It fetches the filename to read from the
fileinput and reads the content into a string - Next, it uses the
gray-matterlibrary to extract the frontmatter - Then for each value in the frontmatter, it sets an output. The key name is run through
slugifyto remove special characters and whitespacea key with spacesbecomesa-key-with-spacest!tlebecomesttle🚀 lift-offbecomeslift-off
As it uses core.setOutput() any complex data types such as objects or arrays are JSON encoded before being output. This means that in the above example, tags will be ["github-actions"].
Common use cases
The best use case that I can think of for this action is automated blog post updates:
yamlname: Get Post Metaon: pushjobs:post-meta:name: Post Metaruns-on: ubuntu-lateststeps:- name: Checkoutuses: actions/checkout@v2- name: Get latest postrun: |cd _postsecho "::set-output name=post_name::$(ls -rt | tail -n 1)"id: latest_post- name: Markdown Metauses: mheap/markdown-meta-action@v1id: metawith:file: ./_posts/${{ steps.latest_post.outputs.post_name }}- uses: ethomson/send-tweet-action@v1with:status: "I just published! ${{ steps.meta.outputs.title }} https://michaelheap.com/${{ steps.meta.outputs.slug }}"consumer-key: ${{ secrets.TWITTER_CONSUMER_API_KEY }}consumer-secret: ${{ secrets.TWITTER_CONSUMER_API_SECRET }}access-token: ${{ secrets.TWITTER_ACCESS_TOKEN }}access-token-secret: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }}