Skip to content

Commit

Permalink
Fix things
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatown committed Nov 3, 2022
1 parent 758a184 commit 7d59ce6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
20 changes: 16 additions & 4 deletions docs/components/Markdoc.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { ElementType, ReactNode } from 'react';
import { RenderableTreeNodes, Scalar, RenderableTreeNode, Tag } from '@markdoc/markdoc';
import type { RenderableTreeNodes, Scalar, RenderableTreeNode, Tag } from '@markdoc/markdoc';
import { isTag } from '../markdoc/isTag';
import { Code, InlineCode } from './primitives/Code';
import { Heading } from './docs/Heading';
import { RelatedContent } from './RelatedContent';
Expand Down Expand Up @@ -51,7 +52,15 @@ export function Markdoc(props: { content: RenderableTreeNodes }) {
return React.createElement(React.Fragment, null, ...node.map(render));
}

if (node === null || typeof node !== 'object') return node;
if (
typeof node === 'string' ||
typeof node === 'number' ||
typeof node === 'boolean' ||
node === null
) {
return node;
}
if (!isTag(node)) return null;

const { name, attributes: { class: className, ...attrs } = {}, children = [] } = node;

Expand Down Expand Up @@ -84,7 +93,7 @@ export type HeadingType = {
export function extractHeadings(content: Tag): HeadingType[] {
const headings: HeadingType[] = [];
for (const child of content.children) {
if (typeof child !== 'string' && child !== null && child.name === 'Heading') {
if (isTag(child) && child.name === 'Heading') {
headings.push({
id: child.attributes.id,
depth: child.attributes.level,
Expand All @@ -99,7 +108,10 @@ function stringifyDocContent(node: RenderableTreeNode): string {
if (typeof node === 'string') {
return node;
}
if (node === null) {
if (Array.isArray(node)) {
return node.map(stringifyDocContent).join('');
}
if (!isTag(node)) {
return '';
}
return node.children.map(stringifyDocContent).join('');
Expand Down
24 changes: 18 additions & 6 deletions docs/markdoc/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
import { RenderableTreeNode } from '@markdoc/markdoc';
import React, { ReactNode } from 'react';
import { isTag } from './isTag';
import { transformContent } from '.';

function renderableToReactElement(node: RenderableTreeNode, key = 1): ReactNode {
if (typeof node === 'string' || node === null) {
if (
typeof node === 'string' ||
typeof node === 'number' ||
typeof node === 'boolean' ||
node === null
) {
return node;
}
return React.createElement(
node.name,
{ ...node.attributes, key },
node.children.map((child, i) => renderableToReactElement(child, i))
);
if (Array.isArray(node)) {
return node.map(renderableToReactElement);
}
if (isTag(node)) {
return React.createElement(
node.name,
{ ...node.attributes, key },
node.children.map((child, i) => renderableToReactElement(child, i))
);
}
return null;
}

expect.addSnapshotSerializer({
Expand Down
5 changes: 3 additions & 2 deletions docs/markdoc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { assert } from 'emery/assertions';
import { load } from 'js-yaml';
import { baseMarkdocConfig } from './config';
import { showNextReleaseWithoutReplacement } from './show-next-release';
import { isTag } from './isTag';

export function printValidationError(error: ValidateError) {
const location = error.error.location || error.location;
Expand Down Expand Up @@ -70,10 +71,10 @@ export function transformContent(errorReportingFilepath: string, content: string
}
const renderableNode = Markdoc.transform(node, markdocConfig);

assert(renderableNode !== null && typeof renderableNode !== 'string');
assert(isTag(renderableNode));

// Next is annoying about not plain objects
return JSON.parse(JSON.stringify(renderableNode)) as typeof renderableNode;
return JSON.parse(JSON.stringify(renderableNode)) as Tag;
}

const frontMatterPattern = /^---[\s]+([\s\S]*?)[\s]+---/;
Expand Down
9 changes: 9 additions & 0 deletions docs/markdoc/isTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Tag } from '@markdoc/markdoc';

// this is Tag.isTag but we don't want to have to load all of markdoc client side
// so it's duplicated
export function isTag(tag: unknown): tag is Tag {
return (
typeof tag === 'object' && tag !== null && '$$mdtype' in tag && (tag as any).$$mdtype === 'Tag'
);
}

0 comments on commit 7d59ce6

Please sign in to comment.