Skip to content

Commit

Permalink
First
Browse files Browse the repository at this point in the history
  • Loading branch information
aarondfrancis committed Aug 27, 2021
0 parents commit d10e884
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/publish.yml
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 }}
15 changes: 15 additions & 0 deletions .github/workflows/tests.yml
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
7 changes: 7 additions & 0 deletions .gitignore
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
5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
torchlight.config.js
source
built
.idea
tests/tmp
1 change: 1 addition & 0 deletions README.md
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.
8 changes: 8 additions & 0 deletions babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"presets": [
"@babel/preset-env"
],
"plugins": [
"@babel/transform-runtime"
]
}
76 changes: 76 additions & 0 deletions index.js
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'
}
43 changes: 43 additions & 0 deletions package.json
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"
}
}
7 changes: 7 additions & 0 deletions tests/__snapshots__/render.test.js.snap
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>
"
`;
40 changes: 40 additions & 0 deletions tests/render.test.js
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);

})

0 comments on commit d10e884

Please sign in to comment.