Skip to content

Commit

Permalink
Markdoc's formatter is experimental and breaks things so use a regex
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatown committed Sep 26, 2022
1 parent 2c5d8e7 commit 8022610
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 48 deletions.
3 changes: 1 addition & 2 deletions docs/markdoc/load-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ export async function loadAllMarkdoc() {
return await Promise.all(
files.map(async file => {
const contents = await fs.readFile(file, 'utf8');
const root = Markdoc.parse(contents, file);
return { file, root };
return { file, contents };
})
);
}
16 changes: 7 additions & 9 deletions docs/markdoc/remove-next-release-conditions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ some content
{% /if %}
`;
expect(removeNextReleaseConditions(Markdoc.parse(content))).toMatchInlineSnapshot(`
expect(removeNextReleaseConditions(content)).toMatchInlineSnapshot(`
{
"contents": "## Heading 1
some content
## Some heading
",
",
"errors": [],
}
`);
Expand All @@ -36,14 +35,13 @@ Content in else
{% /if %}
`;
expect(removeNextReleaseConditions(Markdoc.parse(content))).toMatchInlineSnapshot(`
expect(removeNextReleaseConditions(content)).toMatchInlineSnapshot(`
{
"contents": "## Heading 1
some content
## Some heading
",
",
"errors": [],
}
`);
Expand All @@ -65,16 +63,16 @@ Content in else
{% /if %}
`;
expect(removeNextReleaseConditions(Markdoc.parse(content))).toMatchInlineSnapshot(`
expect(removeNextReleaseConditions(content)).toMatchInlineSnapshot(`
{
"contents": "## Heading 1
{% $nextRelease %}
some content
## Some heading
",
",
"errors": [
{
"error": {
Expand Down
23 changes: 11 additions & 12 deletions docs/markdoc/remove-next-release-conditions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,18 @@ function transform(node: Node): Node[] | Node {
return elseTag === -1 ? node.children : node.children.slice(0, elseTag);
}

export function removeNextReleaseConditions(root: Node) {
const transformed = new Ast.Node(
root.type,
root.attributes,
root.children.flatMap(transform),
root.tag
);
const contents = Markdoc.__EXPERIMENTAL__format(transformed);
const pattern =
/{%\s+if\s+\$nextRelease\s+%}\s*([^]+?)\s*(?:{%\s+else\s+\/%}[^]*?)?{%\s+\/if\s+%}/g;

export function removeNextReleaseConditions(contents: string) {
// ideally this would be a transform
// but Markdoc's formatter is experimental and as of the time of writing this
// doesn't seem to break some of our content
const newContent = contents.replace(pattern, '$1');

// we parse it again before validating so that positions are correct
const reparsed = Markdoc.parse(contents);
// this will report usages of variables in case the transform here is broken
const errors = Markdoc.validate(reparsed, baseMarkdocConfig);
const parsed = Markdoc.parse(newContent);
const errors = Markdoc.validate(parsed, baseMarkdocConfig);

return { contents, errors };
return { contents: newContent, errors };
}
51 changes: 29 additions & 22 deletions docs/pages/docs/fields/virtual.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,43 @@ Options:
Defines what the Admin UI should fetch from this field, it's interpolated into a query like this:
```graphql
query {
item(where: { id: "..." }) {
field${ui.query}
}
}
```

item(where: { id: "..." }) {
field${ui.query}
}

```

}

```

This is only needed when you your field returns a GraphQL type other than a scalar(String and etc.)
or an enum or you need to provide arguments to the field.
or an enum or you need to provide arguments to the field.

```typescript
import { config, createSchema, graphql, list } from '@keystone-6/core';
import { virtual } from '@keystone-6/core/fields';

export default config({
lists: {
SomeListName: list({
fields: {
someFieldName: virtual({
field: graphql.field({
type: graphql.String,
args: { something: graphql.arg({ type: graphql.Int }) },
resolve(item, args, context, info) {

}
})
}),
/* ... */
},
}),
/* ... */
},
lists: {
SomeListName: list({
fields: {
someFieldName: virtual({
field: graphql.field({
type: graphql.String,
args: { something: graphql.arg({ type: graphql.Int }) },
resolve(item, args, context, info) {

}
})
}),
/* ... */
},
}),
/* ... */
},
/* ... */
});
```
4 changes: 2 additions & 2 deletions docs/remove-conditionals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { removeNextReleaseConditions } from './markdoc/remove-next-release-condi
const docs = await loadAllMarkdoc();
const allErrors: ValidateError[] = [];
await Promise.all(
docs.map(({ file, root }) => {
const { contents, errors } = removeNextReleaseConditions(root);
docs.map(({ file, contents: initialContents }) => {
const { contents, errors } = removeNextReleaseConditions(initialContents);
allErrors.push(...errors);
return fs.writeFile(file, contents, 'utf8');
})
Expand Down
3 changes: 2 additions & 1 deletion docs/validate-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const NON_MARKDOWN_PAGES = [
];

(async () => {
const parsedFiles = (await loadAllMarkdoc()).map(({ file, root }) => {
const parsedFiles = (await loadAllMarkdoc()).map(({ file, contents }) => {
const root = Markdoc.parse(contents, file);
const ids = new Set<string>();
for (const node of root.walk()) {
if (node.type === 'heading') {
Expand Down

0 comments on commit 8022610

Please sign in to comment.