Skip to content

Commit

Permalink
Fix using Vite env var names in Markdown (#3412) (#3471)
Browse files Browse the repository at this point in the history
  • Loading branch information
hippotastic committed May 30, 2022
1 parent 429b65d commit 75fa58f
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/old-pigs-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix using Vite env var names in Markdown
13 changes: 10 additions & 3 deletions packages/astro/src/vite-plugin-markdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ export default function markdown({ config }: AstroPluginOptions): Plugin {
return {
code: `
// Static
export const frontmatter = ${JSON.stringify(frontmatter)};
export const frontmatter = ${escapeViteEnvReferences(JSON.stringify(frontmatter))};
export const file = ${JSON.stringify(fileId)};
export const url = ${JSON.stringify(fileUrl)};
export function rawContent() {
return ${JSON.stringify(rawContent)};
return ${escapeViteEnvReferences(JSON.stringify(rawContent))};
}
export async function compiledContent() {
return load().then((m) => m.compiledContent());
Expand Down Expand Up @@ -192,7 +192,7 @@ ${tsResult}`;
sourcefile: id,
});
return {
code,
code: escapeViteEnvReferences(code),
map: null,
};
}
Expand All @@ -201,3 +201,10 @@ ${tsResult}`;
},
};
}

// Converts the first dot in `import.meta.env.` to its Unicode escape sequence,
// which prevents Vite from replacing strings like `import.meta.env.SITE`
// in our JS representation of loaded Markdown files
function escapeViteEnvReferences(code: string) {
return code.replace(/import\.meta\.env\./g, 'import\\u002Emeta.env.');
}
17 changes: 17 additions & 0 deletions packages/astro/test/astro-markdown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,21 @@ describe('Astro Markdown', () => {
`<h2 id="with-components">With components</h2>\n<h3 id="non-hydrated">Non-hydrated</h3>\n<Hello name="Astro Naut" />\n<h3 id="hydrated">Hydrated</h3>\n<Counter client:load />\n<SvelteButton client:load />`
);
});

it('Allows referencing Vite env var names in markdown (#3412)', async () => {
const html = await fixture.readFile('/vite-env-vars/index.html');
const $ = cheerio.load(html);

// test 1: referencing an existing var name
expect($('code').eq(0).text()).to.equal('import.meta.env.SITE');
expect($('li').eq(0).text()).to.equal('import.meta.env.SITE');
expect($('code').eq(2).text()).to.contain('site: import.meta.env.SITE');
expect($('blockquote').text()).to.contain('import.meta.env.SITE');

// test 2: referencing a non-existing var name
expect($('code').eq(1).text()).to.equal('import.meta.env.TITLE');
expect($('li').eq(1).text()).to.equal('import.meta.env.TITLE');
expect($('code').eq(2).text()).to.contain('title: import.meta.env.TITLE');
expect($('blockquote').text()).to.contain('import.meta.env.TITLE');
});
});
3 changes: 2 additions & 1 deletion packages/astro/test/fixtures/astro-markdown/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import svelte from "@astrojs/svelte";

// https://astro.build/config
export default defineConfig({
integrations: [preact(), svelte()]
integrations: [preact(), svelte()],
site: 'https://astro.build/',
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
title: Referencing Vite Env Vars like import.meta.env.SITE and import.meta.env.TITLE
layout: ../layouts/content.astro
---

## Referencing the full name of Vite env vars

You can get the configured site URL with `import.meta.env.SITE`.

The variable `import.meta.env.TITLE` is not configured.

This should also work outside of code blocks:
- import.meta.env.SITE
- import.meta.env.TITLE

## Usage in fenced code blocks with syntax highlighting

```js
// src/pages/rss.xml.js
import rss from '@astrojs/rss';

export const get = () => rss({
site: import.meta.env.SITE,
title: import.meta.env.TITLE,
items: import.meta.glob('./**/*.md'),
});
```

## Usage in frontmatter

> frontmatter.title: {frontmatter.title}

0 comments on commit 75fa58f

Please sign in to comment.