Skip to content
file-text

GitHub Action

Frontmatter to JSON

v0.1.0-alpha Pre-release

Frontmatter to JSON

file-text

Frontmatter to JSON

Convert frontmatter to a JSON string

Installation

Copy and paste the following snippet into your .yml file.

              

- name: Frontmatter to JSON

uses: TheWolfNL/[email protected]

Learn more about this action in TheWolfNL/frontmatter-to-json

Choose a version

Frontmatter to JSON

This is a GitHub Action that converts the frontmatter in a file or content string to a JSON string.

Inputs

  • content: content as string.
  • file: file path as string, it's your responsibility to checkout the code or make sure the file exists.

Outputs

  • json: JSON string containing frontmatter.

Examples

Extract frontmatter from content
...
  - id: content
    name: Extract frontmatter from content
    uses: TheWolfNL/frontmatter-to-json@v1
    with:
      content: '---\ntitle: lorem ipsum\ndescription: hello world\nnumber: 42\ndate: 2024-12-31\n---\n\n# Header 1 #\n\nParagraph 1'

  - name: Output JSON
    shell: bash
    run: |
      echo '${{ steps.content.outputs.json }}' | jq
...
Extract frontmatter from file
...
  - id: file
    name: Extract frontmatter from file
    uses: TheWolfNL/frontmatter-to-json@v1
    with:
      file: 'samples/simple.md'

  - name: Output JSON
    shell: bash
    run: |
      echo '${{ steps.file.outputs.json }}' | jq
...

Usage

Use string value from frontmatter
...
  - id: file
    name: Extract frontmatter from file
    uses: TheWolfNL/frontmatter-to-json@v1
    with:
      file: 'samples/simple.md'

  - name: Output JSON
    shell: bash
    run: echo '${{ fromJson(steps.file.outputs.json).title }}' | jq
...
Use List as input for Matrix
...
jobs:
  data:
    runs-on: ubuntu-latest
    name: Extract list from frontmatter
    permissions:
      contents: read  # This is required for actions/checkout
    outputs:
      keys: ${{ fromJson(steps.file.outputs.json).keys }}
    steps:
      - uses: actions/checkout@v4
      - id: file
        name: Extract frontmatter from file
        uses: TheWolfNL/frontmatter-to-json@v1
        with:
            file: 'samples/list.md'
  keys:
    runs-on: ubuntu-latest
    name: Keys
    needs: data
    if: needs.data.outputs.keys != '[]'
    strategy:
      matrix: 
        key: ${{ fromJson(needs.data.outputs.keys) }}
    steps:
      - name: output
        shell: bash
        run: |
            echo Key: '${{ matrix.key }}'
...