Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing environment variable to the yaml file header #153

Open
elaamrani opened this issue Oct 27, 2022 · 2 comments
Open

Passing environment variable to the yaml file header #153

elaamrani opened this issue Oct 27, 2022 · 2 comments

Comments

@elaamrani
Copy link

Hello,

I would like to pass environment variables to the yaml file header. I tried the following:

---
name: ${{ process.env.ENV_VAR }}
name: `${{ process.env.ENV_VAR }}`
name: ${ENV_VAR}
name: $ENV_VAR
---

None of them worked. Would it be something possible to do?

Thanks!

@webketje
Copy link

Actually you can do this when running gray-matter in Node using the JS parser:

---js
{
  NODE_ENV: process.env.NODE_ENV
}
---

Alternatively you could supply a custom parser engine:

import { javascript } from 'gray-matter/lib/engines.js'

const parsed = matter(str, {
  engines: {
    'embedded-js': (str, options) => {
      // naive implementation but you get the gist
      return javascript.parse(str.replace(/\$/g, 'process.env.'), options)
    }
  }
})

Then use it like so:

---embedded-js
{
  NODE_ENV: $NODE_ENV
}
---

@YiannisBourkelis
Copy link

I would like to do something similar, accessing environment variables on astro docs front matter.

---
title: Page title for product name: {process.env.PUBLIC_ENV_APPNAME}
description: A description
sidebar:
  order: 3
---

Is this somehow possible using gray-matter?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants