Skip to content

Commit 1268701

Browse files
committed
feat: shiki highlighting, content cleanup, component org
1 parent 6fab7ac commit 1268701

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1431
-1670
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,7 @@ yarn-error.log*
3939

4040
.now
4141
.mdx-data
42+
.cache
43+
.yalc
44+
yalc.lock
45+
.cache

.vercelignore

+3
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@
66
node_modules
77
build
88
README.md
9+
.cache
10+
.yalc
11+
yalc.lock

lib/remark-include.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const path = require('path');
2+
const remark = require('remark');
3+
const flatMap = require('unist-util-flatmap');
4+
const { readSync } = require('to-vfile');
5+
6+
module.exports = function includeMarkdownPlugin({ resolveFrom } = {}) {
7+
return function transformer(tree, file) {
8+
return flatMap(tree, node => {
9+
if (node.type !== 'paragraph') return [node];
10+
11+
// detect an `@include` statement
12+
const includeMatch =
13+
node.children[0].value && node.children[0].value.match(/^@include\s['"](.*)['"]$/);
14+
if (!includeMatch) return [node];
15+
16+
// read the file contents
17+
const includePath = path.join(resolveFrom || file.dirname, includeMatch[1]);
18+
let includeContents;
19+
try {
20+
includeContents = readSync(includePath, 'utf8');
21+
} catch (err) {
22+
console.log(err);
23+
throw new Error(
24+
`The @include file path at ${includePath} was not found.\n\nInclude Location: ${file.path}:${node.position.start.line}:${node.position.start.column}`
25+
);
26+
}
27+
28+
// if we are including a ".md" or ".mdx" file, we add the contents as processed markdown
29+
// if any other file type, they are embedded into a code block
30+
if (includePath.match(/\.md(?:x)?$/)) {
31+
// return the file contents in place of the @include
32+
// this takes a couple steps because we allow recursive includes
33+
const processor = remark().use(includeMarkdownPlugin, { resolveFrom });
34+
const ast = processor.parse(includeContents);
35+
return processor.runSync(ast, includeContents).children;
36+
} else {
37+
// trim trailing newline
38+
includeContents.contents = includeContents.contents.trim();
39+
40+
// return contents wrapped inside a "code" node
41+
return [
42+
{
43+
type: 'code',
44+
lang: includePath.match(/\.(\w+)$/)[1],
45+
value: includeContents,
46+
},
47+
];
48+
}
49+
});
50+
};
51+
};

lib/remark-paragraph-alerts.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ module.exports = function paragraphCustomAlertsPlugin() {
1212
return function transformer(tree) {
1313
visit(tree, 'paragraph', (pNode, _, parent) => {
1414
visit(pNode, 'text', textNode => {
15-
Object.keys(sigils).forEach(symbol => {
16-
if (textNode.value.startsWith(`${symbol} `)) {
15+
Object.keys(sigils).forEach(sigil => {
16+
if (textNode.value.startsWith(`${sigil} `)) {
1717
// Remove the literal sigil symbol from string contents
18-
textNode.value = textNode.value.replace(`${symbol} `, '');
18+
textNode.value = textNode.value.replace(`${sigil} `, '');
1919

2020
// Wrap matched nodes with <div> (containing proper attributes)
2121
parent.children = parent.children.map(node => {
@@ -26,7 +26,7 @@ module.exports = function paragraphCustomAlertsPlugin() {
2626
data: {
2727
hName: 'blockquote',
2828
hProperties: {
29-
className: ['alert', `alert-${sigils[symbol]}`],
29+
className: ['alert', `alert-${sigils[sigil]}`],
3030
role: 'alert',
3131
},
3232
},

lib/remark-plugins.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const memoize = require('micro-memoize');
2+
const path = require('path');
3+
4+
const remarkPlugins = [
5+
[require('./remark-include'), { resolveFrom: path.join(__dirname, '../src/includes') }],
6+
require('remark-vscode'),
7+
memoize(require('./remark-paragraph-alerts')),
8+
memoize(require('remark-external-links')),
9+
memoize(require('remark-emoji')),
10+
memoize(require('remark-images')),
11+
memoize(require('remark-unwrap-images')),
12+
memoize(require('remark-normalize-headings')),
13+
memoize(require('remark-slug')),
14+
];
15+
16+
module.exports = { remarkPlugins };

lib/remark-shiki.js

-37
This file was deleted.

next.config.js

+1-25
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,8 @@
11
const withBundleAnalyzer = require('@next/bundle-analyzer')({
22
enabled: process.env.ANALYZE === 'true',
33
});
4-
5-
const webpack = require('webpack');
6-
74
const path = require('path');
8-
9-
const remarkPlugins = [
10-
[
11-
require('@hashicorp/remark-plugins').includeMarkdown,
12-
{ resolveFrom: path.join(__dirname, 'src/_includes') },
13-
],
14-
require('remark-squeeze-paragraphs'),
15-
require('./lib/remark-paragraph-alerts'),
16-
require('remark-external-links'),
17-
require('remark-emoji'),
18-
require('remark-images'),
19-
require('remark-unwrap-images'),
20-
require('remark-normalize-headings'),
21-
require('remark-slug'),
22-
require('remark-footnotes'),
23-
];
5+
const { remarkPlugins } = require('./lib/remark-plugins');
246

257
module.exports = withBundleAnalyzer({
268
experimental: {
@@ -70,12 +52,6 @@ module.exports = withBundleAnalyzer({
7052
const aliases = config.resolve.alias || (config.resolve.alias = {});
7153
aliases.react = aliases['react-dom'] = 'preact/compat';
7254
aliases['react-ssr-prepass'] = 'preact-ssr-prepass';
73-
74-
// to fix a dupe dependency
75-
config.externals.push('prismjs');
76-
77-
// https://github.com/FormidableLabs/react-live#what-bundle-size-can-i-expect
78-
aliases['buble'] = '@philpl/buble';
7955
}
8056

8157
return config;

package.json

+14-6
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
"name": "@blockstack/docs",
33
"version": "1.0.0",
44
"dependencies": {
5-
"@blockstack/ui": "^2.10.7",
5+
"@blockstack/ui": "^2.12.2-beta.0",
66
"@docsearch/react": "^1.0.0-alpha.14",
77
"@hashicorp/remark-plugins": "^3.0.0",
8+
"@mapbox/rehype-prism": "^0.5.0",
89
"@mdx-js/loader": "1.6.13",
910
"@mdx-js/mdx": "^1.6.13",
1011
"@mdx-js/react": "^1.6.13",
@@ -21,26 +22,29 @@
2122
"algoliasearch": "^4.3.1",
2223
"babel-plugin-macros": "^2.8.0",
2324
"babel-plugin-prismjs": "^2.0.1",
25+
"cache-manager": "^3.3.0",
26+
"cache-manager-fs-hash": "^0.0.9",
2427
"csvtojson": "^2.0.10",
2528
"docsearch.js": "^2.6.3",
2629
"eslint": "^7.4.0",
2730
"fathom-client": "^3.0.0",
2831
"front-matter": "^4.0.2",
2932
"fs-extra": "^9.0.1",
30-
"gatsby-remark-shiki": "^0.1.2",
3133
"github-slugger": "^1.3.0",
3234
"gray-matter": "^4.0.2",
3335
"html-react-parser": "^0.13.0",
3436
"jsx-to-string": "^1.4.0",
3537
"lodash.debounce": "^4.0.8",
3638
"mdi-react": "7.3.0",
37-
"next": "^9.4.5-canary.42",
39+
"micro-memoize": "^4.0.9",
40+
"next": "^9.4.5-canary.45",
3841
"next-google-fonts": "^1.1.0",
3942
"next-mdx-enhanced": "^3.0.0",
4043
"next-mdx-remote": "^0.6.0",
4144
"next-optimized-images": "^2.6.1",
4245
"nookies": "^2.3.2",
4346
"nprogress": "^0.2.0",
47+
"p-all": "^3.0.0",
4448
"preact": "^10.4.4",
4549
"preact-render-to-string": "^5.1.4",
4650
"preact-ssr-prepass": "^1.1.0",
@@ -55,7 +59,6 @@
5559
"react-live": "^2.2.2",
5660
"react-simple-code-editor": "^0.11.0",
5761
"react-virtualized-auto-sizer": "^1.0.2",
58-
"react-waypoint": "^9.0.3",
5962
"react-window": "^1.8.5",
6063
"remark": "^12.0.1",
6164
"remark-emoji": "2.1.0",
@@ -64,21 +67,26 @@
6467
"remark-frontmatter": "^2.0.0",
6568
"remark-images": "2.0.0",
6669
"remark-normalize-headings": "^2.0.0",
70+
"remark-parse": "^8.0.3",
6771
"remark-slug": "6.0.0",
6872
"remark-squeeze-paragraphs": "^4.0.0",
6973
"remark-unwrap-images": "2.0.0",
74+
"remark-vscode": "^1.0.0-beta.1",
7075
"store": "^2.0.12",
7176
"strip-markdown": "^3.1.2",
7277
"swr": "^0.2.3",
7378
"touchable-hook": "^1.3.0",
7479
"turndown": "^6.0.0",
7580
"typescript": "^3.9.7",
81+
"unist-builder": "^2.0.3",
7682
"unist-util-is": "^4.0.2",
83+
"unist-util-select": "^3.0.1",
7784
"unist-util-visit": "^2.0.3",
7885
"use-events": "^1.4.2",
7986
"webpack": "^4.43.0"
8087
},
8188
"devDependencies": {
89+
"@babel/preset-react": "^7.10.4",
8290
"@blockstack/eslint-config": "^1.0.5",
8391
"@blockstack/prettier-config": "^0.0.6",
8492
"@next/bundle-analyzer": "^9.4.4",
@@ -92,10 +100,10 @@
92100
},
93101
"private": true,
94102
"scripts": {
95-
"build": "yarn clean:build-files && next telemetry disable && NODE_ENV=production next build && next export -o build",
103+
"build": "yarn clean:build-files && next telemetry disable && NODE_ENV=production next build",
96104
"build:analyze": "yarn clean:build-files && next telemetry disable && NODE_ENV=production ANALYZE=true next build",
97105
"start": "next telemetry disable && NODE_ENV=production next start",
98-
"clean:build-files": "rimraf .next",
106+
"clean:build-files": "rimraf .next && rimraf .cache",
99107
"dev": "yarn clean:build-files && next dev",
100108
"export": "next export",
101109
"lint": "yarn lint:eslint && yarn lint:prettier",

src/common/data/clarity-ref.ts

+77-13
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,86 @@
1-
import { convertRemoteDataToMDX } from '@common/data/mdx';
1+
import { renderMdx } from '@common/data/mdx';
22
import CLARITY_REFERENCE from '../../_data/clarityRef.json';
33

4-
export const convertClarityRefUsageToMdx = async () => {
5-
const [_functions, _keywords] = await Promise.all([
6-
convertRemoteDataToMDX(CLARITY_REFERENCE.functions, 'description'),
7-
convertRemoteDataToMDX(CLARITY_REFERENCE.keywords, 'description'),
8-
]);
4+
const wrapInClarityTicks = (string: string) => {
5+
let newString = '';
6+
newString += '```clarity\n';
7+
newString += string.trim() + '\n';
8+
newString += '```';
9+
return newString;
10+
};
911

10-
const functions = CLARITY_REFERENCE.functions.map((fn, index) => ({
11-
...fn,
12-
description: _functions[index],
13-
}));
12+
const inlineCode = (string: string) => {
13+
let newString = '';
14+
newString += '`';
15+
newString += string.trim();
16+
newString += '`';
17+
return newString;
18+
};
19+
20+
const generateMarkdown = () => {
21+
let keywords = '';
22+
let functions = '';
23+
24+
CLARITY_REFERENCE.functions.forEach(entry => {
25+
functions += `### ${entry.name}
26+
27+
**Signature:** ${inlineCode(entry.signature)}\n
28+
29+
30+
**Input:** ${inlineCode(entry.input_type)}\n
31+
32+
33+
**Output:** ${inlineCode(entry.output_type)}\n
34+
35+
${entry.description}
36+
37+
#### Example
38+
39+
${wrapInClarityTicks(entry.example)}
40+
`;
41+
});
1442

15-
const keywords = CLARITY_REFERENCE.keywords.map((fn, index) => ({
16-
...fn,
17-
description: _keywords[index],
43+
CLARITY_REFERENCE.keywords.forEach(entry => {
44+
keywords += `### ${entry.name}
45+
46+
**Output:** ${inlineCode(entry.output_type)}
47+
48+
${entry.description}
49+
50+
#### Example
51+
52+
${wrapInClarityTicks(entry.example)}
53+
`;
54+
});
55+
56+
return {
57+
keywords,
58+
functions,
59+
};
60+
};
61+
62+
const getHeadings = arr =>
63+
arr.map(entry => ({
64+
content: entry.name,
65+
level: 1,
1866
}));
1967

68+
export const convertClarityRefToMdx = async () => {
69+
const markdown = generateMarkdown();
70+
const [_functions, _keywords] = await Promise.all([
71+
renderMdx(markdown.functions),
72+
renderMdx(markdown.keywords),
73+
]);
74+
75+
const functions = {
76+
content: _functions,
77+
headings: getHeadings(CLARITY_REFERENCE.functions),
78+
};
79+
const keywords = {
80+
content: _keywords,
81+
headings: getHeadings(CLARITY_REFERENCE.keywords),
82+
};
83+
2084
return {
2185
props: {
2286
mdx: {

src/common/data/cli-ref.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ import { convertRemoteDataToMDX } from '@common/data/mdx';
22
import { cliReferenceData } from '../../_data/cliRef';
33

44
export const convertBlockstackCLIUsageToMdx = async () => {
5-
const results = await convertRemoteDataToMDX(cliReferenceData, 'usage');
5+
const transformed = cliReferenceData.map(entry => {
6+
return {
7+
...entry,
8+
};
9+
});
10+
const results = await convertRemoteDataToMDX(transformed, 'usage');
11+
612
return {
713
props: {
814
mdx: results,

0 commit comments

Comments
 (0)