Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-north committed Feb 16, 2019
0 parents commit 65c5d51
Show file tree
Hide file tree
Showing 8 changed files with 6,579 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
yarn-error.log
lib
46 changes: 46 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "mdast-builder",
"description": "Composable functions to easily build mdast structures",
"keywords": [
"mdast"
],
"license": "BSD-2-Clause",
"scripts": {
"build": "tsc -b .",
"build:watch": "yarn build -- --watch",
"test": "mocha",
"test:watch": "mocha --watch"
},
"devDependencies": {
"@commitlint/cli": "7.5.2",
"@commitlint/config-conventional": "7.5.0",
"@commitlint/travis-cli": "7.5.2",
"@mike-north/js-lib-renovate-config": "1.1.1",
"@mike-north/js-lib-semantic-release-config": "1.0.1",
"@types/chai": "^4.1.7",
"@types/mocha": "^5.2.6",
"chai": "^4.2.0",
"husky": "^1.3.1",
"mocha": "^5.2.0",
"nyc": "^13.3.0",
"source-map-support": "^0.5.10",
"ts-node": "^8.0.2",
"typescript": "^3.3.3"
},
"dependencies": {
"@types/unist": "^2.0.3"
},
"commitlint": {
"extends": [
"@commitlint/config-conventional"
]
},
"husky": {
"hooks": {
"commit-msg": "./node_modules/.bin/commitlint -e $HUSKY_GIT_PARAMS"
}
},
"release": {
"extends": "@mike-north/js-lib-semantic-release-config"
}
}
3 changes: 3 additions & 0 deletions renovate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["@mike-north/js-lib-renovate-config"]
}
78 changes: 78 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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') {
const res = children();
return normalizeChildren(res);
} else if (typeof children === 'undefined') {
return [];
} else {
return [children];
}
}

const valueNode = (type: string, value: string): Node => ({
type,
value
});

const nodeWithChildren = (type: string, kids?: Children): Parent => {
const children = kids ? normalizeChildren(kids) : [];

return {
type,
children
};
};

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

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

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

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

export const rootWithTitle = (
depth: number,
title: Children,
kids?: Children
) => root([heading(depth, title), paragraph(kids)]);

export const paragraph = (kids?: Children) =>
nodeWithChildren('paragraph', kids);
export const code = (lang: string, value: string) => ({
...valueNode('code', value),
lang
});

export const heading = (depth: number, kids: Children): Parent => ({
...nodeWithChildren('heading', kids),
depth
});

export const list = (
ordered: 'ordered' | 'unordered',
kids: Children
): Parent => ({
...nodeWithChildren('list', kids),
ordered: ordered === 'ordered'
});
export const listItem = (kids: Children): Parent =>
nodeWithChildren('listItem', kids);
8 changes: 8 additions & 0 deletions test/first.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { suite, test } from 'mocha';
import { expect } from 'chai';

suite('first tests', () => {
test('a thing', () => {
expect(true).to.eq(true);
});
});
5 changes: 5 additions & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
--require ts-node/register
--require source-map-support/register
--timeout 60000
--watch-extensions js,ts,json
**/*.test.ts
11 changes: 11 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "es2015",
"outDir": "lib",
"moduleResolution": "node",
"declaration": true,
"sourceMap": true,
"strict": true
},
"include": ["src"]
}
Loading

0 comments on commit 65c5d51

Please sign in to comment.