Skip to content

Commit

Permalink
feat: separator
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-north committed Feb 22, 2019
1 parent 2cb04f5 commit 6c58a84
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 29 deletions.
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": true
}
54 changes: 28 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Node, Parent } from 'unist';
import { Node, Parent } from "unist";

export type Children = Node | Node[] | (() => Node | Node[]);

function normalizeChildren(children?: Children): Node[] {
if (Array.isArray(children)) {
return children;
} else if (typeof children === 'function') {
} else if (typeof children === "function") {
const res = children();
return normalizeChildren(res);
} else if (typeof children === 'undefined') {
} else if (typeof children === "undefined") {
return [];
} else {
return [children];
Expand All @@ -25,30 +25,32 @@ const nodeWithChildren = (type: string, kids?: Children): Parent => ({
children: normalizeChildren(kids)
});

export const text = (value: string) => valueNode('text', value);
export const inlineCode = (value: string) => valueNode('inlineCode', value);
export const html = (value: string) => valueNode('html', value);
export const text = (value: string) => valueNode("text", value);
export const inlineCode = (value: string) => valueNode("inlineCode", value);
export const html = (value: string) => valueNode("html", value);

export const strong = (kids?: Children) => nodeWithChildren('strong', kids);
export const emphasis = (kids?: Children) => nodeWithChildren('emphasis', kids);
export const strike = (kids?: Children) => nodeWithChildren('delete', kids);
export const strong = (kids?: Children) => nodeWithChildren("strong", kids);
export const emphasis = (kids?: Children) => nodeWithChildren("emphasis", kids);
export const strike = (kids?: Children) => nodeWithChildren("delete", kids);
export const tableCell = (kids?: Children) =>
nodeWithChildren('tableCell', kids);
export const tableRow = (kids?: Children) => nodeWithChildren('tableRow', kids);
nodeWithChildren("tableCell", kids);
export const tableRow = (kids?: Children) => nodeWithChildren("tableRow", kids);
export const table = (
align?: ['left' | 'right', 'left' | 'right' | 'center'],
align?: ["left" | "right", "left" | "right" | "center"],
kids?: Children
) => ({ ...nodeWithChildren('table', kids), align });
) => ({ ...nodeWithChildren("table", kids), align });

export const brk = Object.freeze({ type: 'break' });
export const brk: Node = Object.freeze({ type: "break" });

export const link = (url: string, title: string = '', kids?: Children) => ({
...nodeWithChildren('link', kids),
export const separator: Node = text("---");

export const link = (url: string, title: string = "", kids?: Children) => ({
...nodeWithChildren("link", kids),
url,
title
});

export const root = (kids?: Children) => nodeWithChildren('root', kids);
export const root = (kids?: Children) => nodeWithChildren("root", kids);

export const rootWithTitle = (
depth: number,
Expand All @@ -59,37 +61,37 @@ export const rootWithTitle = (
};

export const paragraph = (kids?: Children) =>
nodeWithChildren('paragraph', kids);
nodeWithChildren("paragraph", kids);

export const image = (
url: string,
title?: string,
alt?: string,
kids?: Children
) => ({ ...nodeWithChildren('image', kids), url, title, alt });
) => ({ ...nodeWithChildren("image", kids), url, title, alt });

export const blockquote = (kids?: Children) =>
nodeWithChildren('blockquote', kids);
nodeWithChildren("blockquote", kids);

export const code = (lang: string, value: string) => ({
...valueNode('code', value),
...valueNode("code", value),
lang
});

export const heading = (depth: number, kids?: Children): Parent => {
if (depth < 1) throw new Error(`Invalid depth: ${depth}`);
return {
...nodeWithChildren('heading', kids),
...nodeWithChildren("heading", kids),
depth
};
};

export const list = (
ordered: 'ordered' | 'unordered',
ordered: "ordered" | "unordered",
kids: Children
): Parent => ({
...nodeWithChildren('list', kids),
ordered: ordered === 'ordered'
...nodeWithChildren("list", kids),
ordered: ordered === "ordered"
});
export const listItem = (kids: Children): Parent =>
nodeWithChildren('listItem', kids);
nodeWithChildren("listItem", kids);
21 changes: 18 additions & 3 deletions test/acceptance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import {
tableCell,
tableRow,
text,
image
image,
separator
} from '../src/index';

var processor = unified().use(stringify, {
Expand Down Expand Up @@ -109,8 +110,22 @@ these are the starting instructions
expectMd(strong(text('foo'))).to.eq('**foo**');
});

it('brk', () => {
expectMd(brk).to.eq(' \n');
it('separator', () => {
expectMd(
root([
heading(1, text('hello')),
paragraph(text('this is a thing')),
separator,
paragraph(text('another thing'))
])
).to.eq(`# hello
this is a thing
---
another thing
`);
});

it('emphasis', () => {
Expand Down

0 comments on commit 6c58a84

Please sign in to comment.