Skip to content

Commit

Permalink
fix frontmatter in Sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
mfix-stripe committed May 11, 2022
1 parent 0e70e54 commit e1fc963
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions components/Sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Markdoc uses a fully declarative approach to composition and flow control, where
- [Explore the syntax](/docs/syntax)
`;

const BASE_FRONTMATTER = { markdoc: { title: '' } };

export function useMarkdocCode(code) {
const ast = React.useMemo(() => Markdoc.parse(code), [code]);

Expand All @@ -43,20 +45,20 @@ export function useMarkdocCode(code) {
// require here to prevent Webpack Promise issue
const yaml = require('js-yaml');

let frontmatter = {};
let frontmatter = BASE_FRONTMATTER;
try {
frontmatter = ast.attributes.frontmatter
? yaml.load(ast.attributes.frontmatter)
: {};
if (ast.attributes.frontmatter) {
frontmatter = yaml.load(ast.attributes.frontmatter);
}
} catch (error) {
//
// pass
}

return {
...rest,
variables: {
markdoc: {
frontmatter
frontmatter: frontmatter || BASE_FRONTMATTER
},
invalid_code: `\n{% callout %}\nHere!\n`
},
Expand Down

1 comment on commit e1fc963

@vercel
Copy link

@vercel vercel bot commented on e1fc963 May 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.