Skip to content

Commit 36990f3

Browse files
committed
First
0 parents  commit 36990f3

File tree

15 files changed

+843
-0
lines changed

15 files changed

+843
-0
lines changed

.github/workflows/publish.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Publish to NPM
2+
on:
3+
release:
4+
types: [ created ]
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v2
10+
- uses: actions/setup-node@v2
11+
with:
12+
node-version: '12.x'
13+
registry-url: 'https://registry.npmjs.org'
14+
- run: npm install
15+
- run: npm run build
16+
- run: npm publish
17+
env:
18+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.github/workflows/tests.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: Tests
2+
on: [ push, pull_request ]
3+
4+
jobs:
5+
test:
6+
runs-on: ubuntu-latest
7+
if: "!contains(github.event.head_commit.message, 'ci skip')"
8+
steps:
9+
- uses: actions/checkout@v2
10+
11+
- name: Install packages
12+
run: npm install
13+
14+
- name: Run tests
15+
run: npm run test

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dist
2+
.idea
3+
node_modules
4+
package-lock.json

cli.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { default as FileCache } from './src/cache/file'
2+
export { default as terminalLogger } from './src/loggers/terminal'

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export { default as Block } from './src/block'
2+
export { default as torchlight } from './src/torchlight'
3+
export { default as MemoryCache } from './src/cache/memory'
4+
export { default as standardLogger } from './src/loggers/standard'

package.json

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"name": "@torchlight-api/client",
3+
"version": "0.0.1",
4+
"description": "A general purpose JavaScript client for Torchlight - the syntax highlighting API",
5+
"source": "*.js",
6+
"type": "module",
7+
"exports": {
8+
".": {
9+
"import": "./dist/client.esm.js",
10+
"require": "./dist/client.cjs"
11+
},
12+
"./cli": {
13+
"import": "./dist/cli.esm.js",
14+
"require": "./dist/cli.cjs"
15+
}
16+
},
17+
"scripts": {
18+
"test": "standard --env jest && jest",
19+
"build": "microbundle -f esm,cjs"
20+
},
21+
"author": "Aaron Francis <[email protected]> (https://torchlight.dev)",
22+
"license": "MIT",
23+
"dependencies": {
24+
"axios": "^0.21.1",
25+
"chalk": "^4.1.2",
26+
"fs-extra": "^10.0.0",
27+
"lodash.chunk": "^4.2.0",
28+
"lodash.get": "^4.4.2",
29+
"md5": "^2.3.0"
30+
},
31+
"devDependencies": {
32+
"@babel/preset-env": "^7.15.0",
33+
"babel-jest": "^27.0.6",
34+
"jest": "^27.0.6",
35+
"microbundle": "^0.13.3",
36+
"standard": "^16.0.3"
37+
},
38+
"jest": {
39+
"clearMocks": true,
40+
"transform": {
41+
"^.+\\.[t|j]sx?$": "babel-jest"
42+
}
43+
},
44+
"babel": {
45+
"presets": [
46+
[
47+
"@babel/preset-env",
48+
{
49+
"targets": {
50+
"node": "12.13.0"
51+
}
52+
}
53+
]
54+
]
55+
},
56+
"standard": {
57+
"ignore": [
58+
"tests",
59+
"dists"
60+
]
61+
}
62+
}

src/block.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import md5 from 'md5'
2+
import guid from './support/guid'
3+
import torchlight from './torchlight'
4+
5+
export default function Block (opts = {}) {
6+
opts = {
7+
id: guid(),
8+
theme: torchlight.config('theme', 'nord'),
9+
...opts
10+
}
11+
12+
this.id = opts.id
13+
this.code = opts.code
14+
this.language = opts.language
15+
this.theme = opts.theme
16+
17+
this.highlighted = null
18+
this.classes = null
19+
this.styles = null
20+
}
21+
22+
Block.prototype.hash = function () {
23+
return md5('' +
24+
this.language +
25+
this.theme +
26+
this.code +
27+
torchlight.config('bust', 0) +
28+
JSON.stringify(torchlight.config('options'))
29+
)
30+
}
31+
32+
Block.prototype.code = function (code) {
33+
this.code = code
34+
35+
return this
36+
}
37+
38+
Block.prototype.language = function (language) {
39+
this.language = language
40+
41+
return this
42+
}
43+
44+
Block.prototype.theme = function (theme) {
45+
this.theme = theme
46+
47+
return this
48+
}
49+
50+
Block.prototype.placeholder = function (extra = '') {
51+
if (extra) {
52+
extra = `-${extra}`
53+
}
54+
55+
return `__torchlight-block-[${this.id}]${extra}__`
56+
}
57+
58+
Block.prototype.setResponseData = function (data) {
59+
if (data) {
60+
this.highlighted = data.highlighted
61+
this.classes = data.classes
62+
this.styles = data.styles
63+
}
64+
65+
return this
66+
}
67+
68+
Block.prototype.setResponseDataFromCache = function () {
69+
const data = this.getResponseDataFromCache()
70+
71+
this.setResponseData(data)
72+
73+
return this
74+
}
75+
76+
Block.prototype.getResponseDataFromCache = function () {
77+
return torchlight.cache.get(this.hash(), {})
78+
}
79+
80+
Block.prototype.populateCache = function (data) {
81+
torchlight.cache.set(this.hash(), data)
82+
83+
return this
84+
}
85+
86+
Block.prototype.toRequestParams = function () {
87+
return {
88+
id: this.id,
89+
hash: this.hash(),
90+
language: this.language,
91+
theme: this.theme,
92+
code: this.code
93+
}
94+
}

src/cache/file.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import md5 from 'md5'
2+
import path from 'path'
3+
import fs from 'fs-extra'
4+
5+
/**
6+
* @param options
7+
* @constructor
8+
*/
9+
export default function File (options = {}) {
10+
if (!options.directory) {
11+
throw new Error('No cache directory specified.')
12+
}
13+
14+
this.directory = path.resolve(options.directory)
15+
16+
fs.ensureDirSync(this.directory)
17+
}
18+
19+
/**
20+
* Get an item from the cache.
21+
*
22+
* @param {string} key
23+
* @param {*} def
24+
* @return {*}
25+
*/
26+
File.prototype.get = function (key, def) {
27+
if (!fs.pathExistsSync(this.filename(key))) {
28+
return def
29+
}
30+
31+
const entry = fs.readJsonSync(this.filename(key))
32+
33+
if (Date.now() / 1000 > entry.expires) {
34+
this.delete(key)
35+
36+
return def
37+
}
38+
39+
return entry.value
40+
}
41+
42+
/**
43+
* Set an item in the cache.
44+
*
45+
* @param {string} key
46+
* @param {*} value
47+
* @param {number} ttlSeconds
48+
*/
49+
File.prototype.set = function (key, value, ttlSeconds = 60 * 24 * 7) {
50+
fs.writeJsonSync(this.filename(key), {
51+
expires: (Date.now() / 1000) + ttlSeconds,
52+
value: value
53+
})
54+
}
55+
56+
/**
57+
* Remove a key from the cache.
58+
*
59+
* @param key
60+
*/
61+
File.prototype.delete = function (key) {
62+
fs.removeSync(this.filename(key))
63+
}
64+
65+
/**
66+
* Clear the cache.
67+
*/
68+
File.prototype.clear = function () {
69+
fs.removeSync(this.directory)
70+
}
71+
72+
/**
73+
* @param {string} key
74+
* @return {string}
75+
*/
76+
File.prototype.filename = function (key) {
77+
return path.join(this.directory, md5(key) + '.json')
78+
}

src/cache/memory.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
let store = {}
2+
3+
export default function Memory () {
4+
//
5+
}
6+
7+
/**
8+
* Get an item from the cache.
9+
*
10+
* @param {string} key
11+
* @param {*} def
12+
* @return {*}
13+
*/
14+
Memory.prototype.get = function (key, def) {
15+
if (!Object.prototype.hasOwnProperty.call(store, key)) {
16+
return def
17+
}
18+
19+
const entry = store[key]
20+
21+
if (Date.now() / 1000 > entry.expires) {
22+
this.delete(key)
23+
24+
return def
25+
}
26+
27+
return entry.value
28+
}
29+
30+
/**
31+
* Set an item in the cache.
32+
*
33+
* @param {string} key
34+
* @param {*} value
35+
* @param {number} ttlSeconds
36+
*/
37+
Memory.prototype.set = function (key, value, ttlSeconds = 60 * 24 * 7) {
38+
store[key] = {
39+
expires: (Date.now() / 1000) + ttlSeconds,
40+
value: value
41+
}
42+
}
43+
44+
/**
45+
* Remove a key from the cache.
46+
*
47+
* @param key
48+
*/
49+
Memory.prototype.delete = function (key) {
50+
delete store[key]
51+
}
52+
53+
/**
54+
* Clear the cache.
55+
*/
56+
Memory.prototype.clear = function () {
57+
store = {}
58+
}

0 commit comments

Comments
 (0)