Skip to content

Adds syntax highlighting to the code #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "node",
"request": "launch",
"protocol": "inspector",
"program": "${workspaceRoot}/node_modules/gatsby/dist/bin/gatsby",
"program": "${workspaceRoot}/node_modules/gatsby/dist/bin/gatsby.js",
"args": ["develop"],
"cwd": "${workspaceFolder}/packages/typescriptlang-org",
"stopOnEntry": false,
Expand All @@ -18,7 +18,7 @@
"type": "node",
"request": "launch",
"protocol": "inspector",
"program": "${workspaceRoot}/node_modules/gatsby/dist/bin/gatsby",
"program": "${workspaceRoot}/node_modules/gatsby/dist/bin/gatsby.js",
"args": ["build"],
"cwd": "${workspaceFolder}/packages/typescriptlang-org",
"stopOnEntry": false,
Expand Down
21 changes: 21 additions & 0 deletions packages/gatsby-remark-shiki/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
### gatsby-remark-shiki

Sets up code blocks to run through shiki:

```js
{
resolve: `gatsby-transformer-remark`,
options: {
plugins: [
"gatsby-remark-twoslasher-code-blocks",
"gatsby-remark-autolink-headers",
"gatsby-remark-shiki",
"gatsby-remark-copy-linked-files",
"gatsby-remark-smartypants",
],
},
}
```

You want this really late in the plugin process if you have anything else touching your code, as after this
the code will be raw HTML.
66 changes: 66 additions & 0 deletions packages/gatsby-remark-shiki/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// @ts-enable

const shiki = require('shiki')
const visit = require('unist-util-visit')

const { commonLangIds, commonLangAliases, otherLangIds } = require('shiki-languages')
const languages = [...commonLangIds, ...commonLangAliases, ...otherLangIds]

/**
* This gets filled in by the promise below, then should
* hopefully be more or less synchronous access by each parse
* of the highlighter
*/
let highlighter = null

const getHighlighter = options => {
if (highlighter) return highlighter

var settings = options || {}
var theme = settings.theme || 'nord'
var shikiTheme

try {
shikiTheme = shiki.getTheme(theme)
} catch (error) {
try {
shikiTheme = shiki.loadTheme(theme)
} catch (error) {
throw new Error('Unable to load theme: ' + theme)
}
}

return shiki.getHighlighter({ theme: shikiTheme, langs: languages }).then(newHighlighter => {
highlighter = newHighlighter
return highlighter
})
}

/**
* The function doing the work of transforming any codeblock samples
* which have opted-in to the twoslash pattern.
*
* @param {Node} node
*/
const visitor = node => {
let lang = node.lang
const replacer = {
"json5": "json"
}

if (replacer[lang]) lang = replacer[lang]

const shouldHighlight = lang && languages.includes(lang)
if (shouldHighlight) {
const results = highlighter.codeToHtml(node.value, lang)
node.type = "html"
node.value = results
node.children = []
}
}

/** The plugin API */
module.exports = async ({ markdownAST }) => {
await getHighlighter()
visit(markdownAST, 'code', visitor)
}
12 changes: 12 additions & 0 deletions packages/gatsby-remark-shiki/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "gatsby-remark-shiki",
"version": "0.1.0",
"license": "MIT",
"dependencies": {
"shiki": "~0.1.6"
},
"scripts": {
"build": "echo 'NOOP'",
"test": "echo 'NOOP'"
}
}
27 changes: 27 additions & 0 deletions packages/gatsby-remark-shiki/scripts/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## Some markdown

I shouldn't get changed:

```ts
// @showEmit
export function fn(arr: number[]) {
const arr2 = [1, ...arr];
}
```

I should:

```ts twoslash
// @showEmit
// @target: ES5
// @downleveliteration
// @importhelpers

// --importHelpers on: Spread helper will be imported from 'tslib'

export function fn(arr: number[]) {
const arr2 = [1, ...arr];
}
```

Cool, a paragraph after
8 changes: 8 additions & 0 deletions packages/gatsby-remark-shiki/scripts/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const remark = require('remark')
const gatsbyRemarkShiki = require('../')
const { readFileSync } = require('fs')

const markdownAST = remark().parse(readFileSync(__dirname + '/index.md', 'utf8'))
gatsbyRemarkShiki({ markdownAST }).then(() => {
console.log(markdownAST)
})
2 changes: 1 addition & 1 deletion packages/handbook-v1/en/Advanced Types.md
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ button.animate(0, 0, "uneasy"); // error: "uneasy" is not allowed here

You can pass any of the three allowed strings, but any other string will give the error

```text
```
Argument of type '"uneasy"' is not assignable to parameter of type '"ease-in" | "ease-out" | "ease-in-out"'
```

Expand Down
4 changes: 2 additions & 2 deletions packages/handbook-v1/en/Classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ In TypeScript, we allow developers to use these techniques now, and compile them

# Classes

Let's take a look at a simple class-based example:
Let's take a look at a simple class-based example

```ts
class Greeter {
Expand Down Expand Up @@ -113,7 +113,7 @@ The example also shows how to override methods in the base class with methods th
Here both `Snake` and `Horse` create a `move` method that overrides the `move` from `Animal`, giving it functionality specific to each class.
Note that even though `tom` is declared as an `Animal`, since its value is a `Horse`, calling `tom.move(34)` will call the overriding method in `Horse`:

```Text
```
Slithering...
Sammy the Python moved 5m.
Galloping...
Expand Down
6 changes: 3 additions & 3 deletions packages/handbook-v1/en/Variable Declarations.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ For those unfamiliar, `setTimeout` will try to execute a function after a certai

Ready? Take a look:

```text
```
10
10
10
Expand All @@ -146,7 +146,7 @@ Ready? Take a look:
Many JavaScript developers are intimately familiar with this behavior, but if you're surprised, you're certainly not alone.
Most people expect the output to be

```text
```
0
1
2
Expand Down Expand Up @@ -374,7 +374,7 @@ for (let i = 0; i < 10 ; i++) {

and as expected, this will print out

```text
```
0
1
2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ The layout of your declaration files should mirror the layout of the library.

A library can consist of multiple modules, such as

```Text
```
myLib
+---- index.js
+---- foo.js
Expand All @@ -395,7 +395,7 @@ var d = require("myLib/bar/baz");

Your declaration files should thus be

```Text
```
@types/myLib
+---- index.d.ts
+---- foo.d.ts
Expand Down
2 changes: 1 addition & 1 deletion packages/handbook-v1/en/tutorials/Gulp.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ cd proj

To start, we're going to structure our project in the following way:

```text
```
proj/
├─ src/
└─ dist/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ In this case, you might already have a folder structure like this set up.

From this point on, we're going to assume that your directory is set up something like this:

```text
```
projectRoot
├── src
│ ├── file1.js
Expand Down
2 changes: 1 addition & 1 deletion packages/handbook-v1/en/tutorials/React & Webpack.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ cd proj

To start, we're going to structure our project in the following way:

```text
```
proj/
├─ dist/
└─ src/
Expand Down
2 changes: 1 addition & 1 deletion packages/handbook-v1/en/tutorials/React.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ create-react-app my-app --scripts-version=react-scripts-ts

At this point, your project layout should look like the following:

```text
```
my-app/
├─ .gitignore
├─ node_modules/
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions packages/typescriptlang-org/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ module.exports = {
resolve: `gatsby-transformer-remark`,
options: {
plugins: [
`${require.resolve("gatsby-remark-twoslasher-code-blocks")}`,
require.resolve("gatsby-remark-twoslasher-code-blocks"),
{
resolve: `gatsby-remark-images`,
options: {
Expand All @@ -71,7 +71,7 @@ module.exports = {
},
},
"gatsby-remark-autolink-headers",
"gatsby-remark-prismjs",
require.resolve("gatsby-remark-shiki"),
"gatsby-remark-copy-linked-files",
"gatsby-remark-smartypants",
],
Expand Down
2 changes: 2 additions & 0 deletions packages/typescriptlang-org/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"gatsby-remark-images": "^3.1.30",
"gatsby-remark-prismjs": "^3.3.22",
"gatsby-remark-responsive-iframe": "^2.2.26",
"gatsby-remark-shiki": "0.1.0",
"gatsby-remark-smartypants": "^2.1.15",
"gatsby-remark-twoslasher-code-blocks": "0.1.0",
"gatsby-source-filesystem": "^2.1.36",
Expand All @@ -41,6 +42,7 @@
"react": "^16.11.0",
"react-dom": "^16.11.0",
"react-helmet": "^5.2.1",
"rehype-shiki": "^0.0.5",
"ts-node": "^8.5.0",
"ts-twoslasher": "0.1.0",
"typescript": "^3.7.2"
Expand Down
Loading