Skip to content

Commit b89cf38

Browse files
committed
docs(lib): add lib v1.0.0
1 parent d6f8823 commit b89cf38

File tree

6 files changed

+139
-0
lines changed

6 files changed

+139
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { removeFromLast } from './utils';
2+
// @ts-ignore
3+
export function findRouteByPath(path, routes) {
4+
// eslint-disable-next-line
5+
for (const route of routes) {
6+
if (route.path && removeFromLast(route.path, '.') === path) {
7+
return route;
8+
}
9+
10+
const childPath = route.routes && findRouteByPath(path, route.routes);
11+
if (childPath) return childPath;
12+
}
13+
}

docs/src/lib/docs/md-loader.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const fm = require('gray-matter')
2+
3+
// makes mdx in next.js suck less by injecting necessary exports so that
4+
// the docs are still readable on github
5+
// (Shamelessly stolen from Expo.io docs)
6+
// @see https://github.com/expo/expo/blob/master/docs/common/md-loader.js
7+
module.exports = async function (src) {
8+
const callback = this.async()
9+
const { content, data } = fm(src)
10+
const layout = data.layout || 'Docs'
11+
const code =
12+
`import { Layout${layout} } from 'components/Layout${layout}';
13+
export const meta = ${JSON.stringify(data)};
14+
export default ({ children, ...props }) => (
15+
<Layout${layout} meta={meta} {...props}>{children}</Layout${layout}>
16+
);
17+
18+
19+
` + content
20+
21+
return callback(null, code)
22+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const is = require('unist-util-is');
2+
const visit = require('unist-util-visit');
3+
4+
const sigils = {
5+
'=>': 'success',
6+
'->': 'info',
7+
'~>': 'warning',
8+
'!>': 'danger',
9+
};
10+
11+
module.exports = function paragraphCustomAlertsPlugin() {
12+
return function transformer(tree) {
13+
visit(tree, 'paragraph', (pNode, _, parent) => {
14+
visit(pNode, 'text', textNode => {
15+
Object.keys(sigils).forEach(symbol => {
16+
if (textNode.value.startsWith(`${symbol} `)) {
17+
// Remove the literal sigil symbol from string contents
18+
textNode.value = textNode.value.replace(`${symbol} `, '');
19+
20+
// Wrap matched nodes with <div> (containing proper attributes)
21+
parent.children = parent.children.map(node => {
22+
return is(pNode, node)
23+
? {
24+
type: 'wrapper',
25+
children: [node],
26+
data: {
27+
hName: 'div',
28+
hProperties: {
29+
className: [
30+
'alert',
31+
`alert-${sigils[symbol]}`,
32+
'g-type-body',
33+
],
34+
role: 'alert',
35+
},
36+
},
37+
}
38+
: node;
39+
});
40+
}
41+
});
42+
});
43+
});
44+
};
45+
};

docs/src/lib/docs/utils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export function removeFromLast(path, key) {
2+
const i = path.lastIndexOf(key)
3+
return i === -1 ? path : path.substring(0, i)
4+
}

docs/src/lib/fs-utils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import fs from 'fs';
2+
import { promisify } from 'util';
3+
export const readFile = promisify(fs.readFile);
4+
export const writeFile = promisify(fs.writeFile);

docs/src/lib/get-route-context.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Returns the siblings of a specific route (that is the previous and next routes).
3+
*/
4+
export function getRouteContext(_route, routes, ctx = {}) {
5+
if (!_route) {
6+
return ctx;
7+
}
8+
9+
const {
10+
path
11+
} = _route;
12+
const {
13+
parent
14+
} = ctx;
15+
16+
for (let i = 0; i < routes.length; i += 1) {
17+
const route = routes[i];
18+
19+
if (route.routes) {
20+
ctx.parent = route;
21+
ctx = getRouteContext(_route, route.routes, ctx); // If the active route and the next route was found in nested routes, return it
22+
23+
if (ctx.nextRoute) return ctx;
24+
}
25+
26+
if (!route) continue;
27+
if (!route.path) continue;
28+
29+
if (ctx.route) {
30+
ctx.nextRoute = parent && i === 0 ? { ...route,
31+
title: `${parent.title}: ${route.title}`
32+
} : route;
33+
return ctx;
34+
}
35+
36+
if (route && route.path === path) {
37+
ctx.route = { ..._route,
38+
title: parent && !parent.heading ? `${parent.title}: ${_route.title}` : _route.title
39+
}; // Continue the loop until we know the next route
40+
41+
continue;
42+
}
43+
44+
ctx.prevRoute = parent && !parent.heading && !routes[i + 1]?.path ? { ...route,
45+
title: `${parent.title}: ${route.title}`
46+
} : route;
47+
} // The loop ended and the previous route was found, or nothing
48+
49+
50+
return ctx;
51+
}

0 commit comments

Comments
 (0)