-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d10e884
Showing
10 changed files
with
220 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
name: Publish to NPM | ||
on: | ||
release: | ||
types: [ created ] | ||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
# Setup .npmrc file to publish to npm | ||
- uses: actions/setup-node@v2 | ||
with: | ||
node-version: '12.x' | ||
registry-url: 'https://registry.npmjs.org' | ||
- run: npm install | ||
- run: npm publish | ||
env: | ||
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
name: Tests | ||
on: [ push, pull_request ] | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
if: "!contains(github.event.head_commit.message, 'ci skip')" | ||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Install packages | ||
run: npm install | ||
|
||
- name: Run tests | ||
run: npm run test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
torchlight.config.js | ||
source | ||
built | ||
.idea | ||
node_modules | ||
tests/tmp | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
torchlight.config.js | ||
source | ||
built | ||
.idea | ||
tests/tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
See [torchlight.dev/docs/clients/cli](https://torchlight.dev/docs/clients/cli) for full docs. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"presets": [ | ||
"@babel/preset-env" | ||
], | ||
"plugins": [ | ||
"@babel/transform-runtime" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import parse5 from 'parse5' | ||
import { map } from 'unist-util-map' | ||
import { fromParse5 } from 'hast-util-from-parse5' | ||
|
||
import { torchlight, Block } from '@torchlight-api/torchlight-cli' | ||
|
||
export default function plugin (options = {}) { | ||
torchlight.init(options.config) | ||
|
||
return tree => transform(tree, options) | ||
} | ||
|
||
function transform (tree, options) { | ||
const blocks = [] | ||
|
||
const withPlaceholders = map(tree, node => { | ||
if (!isBlockCode(node)) { | ||
return node | ||
} | ||
|
||
const block = new Block({ | ||
language: node.lang, | ||
code: node.value | ||
}) | ||
|
||
blocks.push(block) | ||
|
||
node.data = node.data ?? {} | ||
node.data.torchlight = block | ||
|
||
return node | ||
}) | ||
|
||
return torchlight.highlight(blocks).then(() => { | ||
return map(withPlaceholders, node => { | ||
if (!node?.data?.torchlight) { | ||
return node | ||
} | ||
|
||
const block = node.data.torchlight | ||
const hast = fromParse5(parse5.parse(block.highlighted)) | ||
|
||
// The fragment that is returned from the API gets wrapped in a | ||
// document and body, so we need to find the actual content | ||
// inside all of the wrappers. | ||
const highlighted = hast.children.pop().children.find(child => child.tagName === 'body').children | ||
|
||
const code = h('code', { | ||
className: `language-${block.language}` | ||
}, highlighted) | ||
|
||
return h('pre', { | ||
className: [block.classes], | ||
style: block.styles | ||
}, [code]) | ||
}) | ||
}) | ||
} | ||
|
||
const h = (type, attrs = {}, children = []) => { | ||
return { | ||
type: 'element', | ||
tagName: type, | ||
data: { | ||
hName: type, | ||
hProperties: attrs, | ||
hChildren: children | ||
}, | ||
properties: attrs, | ||
children | ||
} | ||
} | ||
|
||
function isBlockCode (node) { | ||
return node.type === 'code' || node.tagName === 'code' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"name": "remark-torchlight", | ||
"version": "0.0.1", | ||
"description": "A remark plugin for Torchlight - the syntax highlighting API.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "standard --env jest && jest" | ||
}, | ||
"keywords": [ | ||
"syntax", | ||
"highlighting", | ||
"torchlight" | ||
], | ||
"standard": { | ||
"ignore": [ | ||
"tests" | ||
] | ||
}, | ||
"jest": { | ||
"clearMocks": true, | ||
"transform": { | ||
"^.+\\.[t|j]sx?$": "babel-jest" | ||
}, | ||
"transformIgnorePatterns": [] | ||
}, | ||
"author": "Aaron Francis <[email protected]> (https://torchlight.dev)", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@torchlight-api/torchlight-cli": "^0.1.2", | ||
"hast-util-from-parse5": "^7.1.0", | ||
"parse5": "^6.0.1", | ||
"unist-util-map": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"@babel/plugin-transform-runtime": "^7.15.0", | ||
"@babel/preset-env": "^7.15.0", | ||
"babel-jest": "^27.0.6", | ||
"jest": "^27.0.6", | ||
"remark": "^14.0.1", | ||
"remark-html": "^14.0.0", | ||
"standard": "^16.0.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`it tests 1`] = ` | ||
"<p>this <em>is</em> <a href=\\"https://www.google.com\\">test</a></p> | ||
<pre class=\\"classes\\" style=\\"style: 1;\\"><code class=\\"language-js\\">highlighted</code></pre> | ||
" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import torchlight from '../index' | ||
import { remark } from 'remark' | ||
import html from 'remark-html' | ||
import {mockApi} from '@torchlight-api/torchlight-cli/tests/support/helpers' | ||
|
||
process.env.TORCHLIGHT_TOKEN = 'test' | ||
|
||
test('it tests', async () => { | ||
|
||
const markdown = `this _is_ [test](https://www.google.com) | ||
\`\`\`js | ||
// hello | ||
\`\`\` | ||
` | ||
|
||
const mock = mockApi(data => { | ||
expect(data.blocks).toHaveLength(1) | ||
|
||
const block = data.blocks[0] | ||
|
||
expect(block.code).toEqual('// hello') | ||
expect(block.language).toEqual('js') | ||
|
||
return [{ | ||
...block, | ||
highlighted: 'highlighted', | ||
classes: 'classes', | ||
styles: 'style: 1;' | ||
}] | ||
}) | ||
|
||
const result = await remark() | ||
.use(html) | ||
.use(torchlight) | ||
.process(markdown) | ||
|
||
expect(result.toString()).toMatchSnapshot() | ||
expect(mock).toHaveBeenCalledTimes(1); | ||
|
||
}) |