Skip to content

Commit d5a4ce4

Browse files
author
Orta
authored
Merge pull request #131 from microsoft/gatsby-twoslash
Adds twoslash to the gatsby side
2 parents f6a100e + 1a1f43a commit d5a4ce4

File tree

25 files changed

+275
-15963
lines changed

25 files changed

+275
-15963
lines changed

.vscode/settings.json

+1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414
},
1515
"peacock.color": "#3178C6",
1616
"typescript.tsdk": "node_modules/typescript/lib",
17+
"debug.node.autoAttach": "off",
1718
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"husky": "^3.0.9"
1313
},
1414
"scripts": {
15-
"bootstrap": "yarn build-tsconfig-reference-md",
15+
"bootstrap": "yarn workspaces run build",
1616
"start": "yarn workspace typescriptlang-org start",
1717
"build-site": "yarn workspace typescriptlang-org build",
1818
"build-tsconfig-reference-md": "yarn workspace tsconfig-reference build",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
### gatsby-remark-twoslasher-code-blocks
2+
3+
Sets up code blocks to run through ts-twoslasher:
4+
5+
```js
6+
{
7+
resolve: `gatsby-transformer-remark`,
8+
options: {
9+
plugins: [
10+
"gatsby-remark-twoslasher-code-blocks",
11+
{
12+
resolve: `gatsby-remark-images`,
13+
options: {
14+
maxWidth: 590,
15+
},
16+
},
17+
{
18+
resolve: `gatsby-remark-responsive-iframe`,
19+
options: {
20+
wrapperStyle: `margin-bottom: 1.0725rem`,
21+
},
22+
},
23+
"gatsby-remark-autolink-headers",
24+
"gatsby-remark-prismjs",
25+
"gatsby-remark-copy-linked-files",
26+
"gatsby-remark-smartypants",
27+
],
28+
},
29+
}
30+
```
31+
32+
Currently it only works when you specify twoslash in your codeblock
33+
34+
```js
35+
// Nothing will happen
36+
'''ts
37+
// @showEmit
38+
// @target ES5
39+
const staysAsTypeScript = true
40+
'''
41+
42+
// This will convert
43+
'''ts twoslash
44+
// @showEmit
45+
// @target ES5
46+
47+
const staysAsTypeScript = false
48+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// @ts-enable
2+
3+
const visit = require('unist-util-visit');
4+
const {twoslasher} = require(require.resolve("ts-twoslasher"))
5+
6+
/**
7+
* The function doing the work of transforming any codeblock samples
8+
* which have opted-in to the twoslash pattern.
9+
*
10+
* @param {Node} node
11+
*/
12+
const visitor = (node) => {
13+
if (!node.meta || !node.meta.includes("twoslash")) return
14+
15+
const results = twoslasher(node.value, node.lang)
16+
node.value = results.code
17+
node.lang = results.extension
18+
node.twoslash = {
19+
highlights: results.highlights,
20+
playgroundURL: results.playgroundURL,
21+
queries: results.queries,
22+
}
23+
}
24+
25+
/** The plugin API */
26+
module.exports = ({ markdownAST }) => {
27+
visit(markdownAST, 'code', visitor);
28+
};
29+
30+
module.exports.visitor = visitor
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "gatsby-remark-twoslasher-code-blocks",
3+
"version": "0.1.0",
4+
"license": "MIT",
5+
"dependencies": {
6+
"ts-twoslasher": "0.1.0",
7+
"unist-util-visit": "^2.0.1"
8+
},
9+
"scripts": {
10+
"build": "echo 'NOOP'",
11+
"test": "echo 'NOOP'"
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## Some markdown
2+
3+
I shouldn't get changed:
4+
5+
```ts
6+
// @showEmit
7+
export function fn(arr: number[]) {
8+
const arr2 = [1, ...arr];
9+
}
10+
```
11+
12+
I should:
13+
14+
```ts twoslash
15+
// @showEmit
16+
// @target: ES5
17+
// @downleveliteration
18+
// @importhelpers
19+
20+
// --importHelpers on: Spread helper will be imported from 'tslib'
21+
22+
export function fn(arr: number[]) {
23+
const arr2 = [1, ...arr];
24+
}
25+
```
26+
27+
Cool, a paragraph after
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var remark = require("remark")
2+
var visit = require("unist-util-visit")
3+
4+
const { readFileSync }= require("fs")
5+
6+
var remark = require("remark")
7+
var visit = require("unist-util-visit")
8+
var {visitor} = require("../")
9+
10+
var tree = remark().parse(readFileSync(__dirname + "/index.md", "utf8"))
11+
visit(tree, "code", visitor)
12+
console.log(tree)

packages/handbook-v1/package.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "handbook-v1",
3+
"private": true,
4+
"license": "MIT",
5+
"version": "1.0.0",
6+
"scripts": {
7+
"build": "echo 'NOOP'",
8+
"test": "echo 'NOOP'"
9+
}
10+
}

packages/playground/.npmignore

-3
This file was deleted.

packages/playground/README.md

-1
This file was deleted.

packages/playground/index.html

-14
This file was deleted.

packages/playground/index.tsx

-14
This file was deleted.

packages/playground/package.json

-20
This file was deleted.

packages/playground/tsconfig.json

-19
This file was deleted.

0 commit comments

Comments
 (0)