Skip to content

Commit

Permalink
feat: export markdown builder (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
lottamus authored Jul 16, 2021
1 parent a48880b commit 215526f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/ast-types/mdast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ export type TabsContent = Tab;

export interface Tab extends Parent {
type: 'tab';

annotations?: {
title?: String;
};
}

export interface CodeGroup extends Parent<Code> {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './ast-types';
export * from './builder';
export * from './frontmatter';
export * from './getJsonPathForNode';
export * from './getJsonPathForPosition';
Expand Down
11 changes: 8 additions & 3 deletions src/plugins/stringify/blockquote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@ import { Handler } from 'mdast-util-to-markdown';
// @ts-expect-error
import blockquote from 'mdast-util-to-markdown/lib/handle/blockquote';

import { MDAST } from '../../ast-types';

const { safeStringify } = Yaml;

export const blockquoteHandler: Handler = function (node, _, context) {
const annotations = (node.data?.hProperties || {}) as any;
const annotations = {
...(node.annotations as MDAST.Blockquote['annotations']),
...(node.data?.hProperties as MDAST.Blockquote['annotations']),
};

const value = blockquote(node, _, context);

if (Object.keys(annotations).length) {
return `<!-- ${safeStringify(annotations, { skipInvalid: true }).trim()} -->
${value}`;
} else {
return value;
}

return value;
};
4 changes: 3 additions & 1 deletion src/plugins/stringify/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Handler } from 'mdast-util-to-markdown';
// @ts-expect-error
import code from 'mdast-util-to-markdown/lib/handle/code';

import { MDAST } from '../../ast-types';

const { safeStringify } = Yaml;

export const codeHandler: Handler = function (node, _, context) {
Expand All @@ -14,7 +16,7 @@ export const codeHandler: Handler = function (node, _, context) {
node.lang === 'json' ? JSON.stringify(node.resolved, null, 2) : safeStringify(node.resolved, { indent: 2 });
}

const metaProps = computeMetaProps(annotations);
const metaProps = computeMetaProps({ ...(node.annotations as MDAST.Code['annotations']), ...annotations });
if (metaProps.length) {
node.meta = metaProps.join(' ');
}
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/stringify/tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Handler } from 'mdast-util-to-markdown';
// @ts-expect-error
import flow from 'mdast-util-to-markdown/lib/util/container-flow';

import { MDAST } from '../../ast-types';

const { safeStringify } = Yaml;

export const tabsHandler: Handler = function (node, _, context) {
Expand All @@ -25,7 +27,7 @@ export const tabHandler: Handler = function (node, _, context) {

return `<!--
type: tab
${safeStringify(annotations, { skipInvalid: true }).trim()}
${safeStringify({ ...(node.annotations as MDAST.Tab['annotations']), ...annotations }, { skipInvalid: true }).trim()}
-->
${value}`;
Expand Down

0 comments on commit 215526f

Please sign in to comment.