Skip to content

Commit

Permalink
Add common build, default to it
Browse files Browse the repository at this point in the history
* There’s now `core` (0 languages), `common` (35 languages), and
  `all` (237 languages)
* This switches the default import to use *common*
  • Loading branch information
wooorm committed May 8, 2021
1 parent 0481750 commit b73ea4c
Show file tree
Hide file tree
Showing 13 changed files with 734 additions and 622 deletions.
488 changes: 6 additions & 482 deletions index.js

Large diffs are not rendered by default.

484 changes: 484 additions & 0 deletions lib/all.js

Large diffs are not rendered by default.

82 changes: 82 additions & 0 deletions lib/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* @typedef {import('./core.js').RefractorRoot} RefractorRoot
* @typedef {import('./core.js').RefractorElement} RefractorElement
* @typedef {import('./core.js').Text} Text
* @typedef {import('./core.js').Grammar} Grammar
* @typedef {import('./core.js').Syntax} Syntax
*/
import {refractor} from './core.js'
import clike from '../lang/clike.js'
import c from '../lang/c.js'
import cpp from '../lang/cpp.js'
import arduino from '../lang/arduino.js'
import bash from '../lang/bash.js'
import csharp from '../lang/csharp.js'
import markup from '../lang/markup.js'
import css from '../lang/css.js'
import diff from '../lang/diff.js'
import go from '../lang/go.js'
import ini from '../lang/ini.js'
import java from '../lang/java.js'
import regex from '../lang/regex.js'
import javascript from '../lang/javascript.js'
import json from '../lang/json.js'
import kotlin from '../lang/kotlin.js'
import less from '../lang/less.js'
import lua from '../lang/lua.js'
import makefile from '../lang/makefile.js'
import yaml from '../lang/yaml.js'
import markdown from '../lang/markdown.js'
import objectivec from '../lang/objectivec.js'
import perl from '../lang/perl.js'
import markupTemplating from '../lang/markup-templating.js'
import php from '../lang/php.js'
import python from '../lang/python.js'
import r from '../lang/r.js'
import ruby from '../lang/ruby.js'
import rust from '../lang/rust.js'
import sass from '../lang/sass.js'
import scss from '../lang/scss.js'
import sql from '../lang/sql.js'
import swift from '../lang/swift.js'
import typescript from '../lang/typescript.js'
import basic from '../lang/basic.js'
import vbnet from '../lang/vbnet.js'

export {refractor}
refractor.register(clike)
refractor.register(c)
refractor.register(cpp)
refractor.register(arduino)
refractor.register(bash)
refractor.register(csharp)
refractor.register(markup)
refractor.register(css)
refractor.register(diff)
refractor.register(go)
refractor.register(ini)
refractor.register(java)
refractor.register(regex)
refractor.register(javascript)
refractor.register(json)
refractor.register(kotlin)
refractor.register(less)
refractor.register(lua)
refractor.register(makefile)
refractor.register(yaml)
refractor.register(markdown)
refractor.register(objectivec)
refractor.register(perl)
refractor.register(markupTemplating)
refractor.register(php)
refractor.register(python)
refractor.register(r)
refractor.register(ruby)
refractor.register(rust)
refractor.register(sass)
refractor.register(scss)
refractor.register(sql)
refractor.register(swift)
refractor.register(typescript)
refractor.register(basic)
refractor.register(vbnet)
File renamed without changes.
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
"types": "index.d.ts",
"files": [
"lang/",
"core.d.ts",
"core.js",
"lib/",
"index.d.ts",
"index.js"
],
Expand All @@ -49,9 +48,9 @@
},
"devDependencies": {
"@babel/core": "^7.0.0",
"@types/async": "^3.2.6",
"@types/babel__core": "^7.1.14",
"@types/mdast": "^3.0.3",
"@types/async": "^3.0.0",
"@types/babel__core": "^7.0.0",
"@types/mdast": "^3.0.0",
"@types/tape": "^4.0.0",
"alpha-sort": "^5.0.0",
"async": "^3.0.0",
Expand All @@ -76,7 +75,7 @@
},
"scripts": {
"prepack": "npm run build && npm run format",
"build": "rimraf \"{lang/**,script/**,test/**,}*.d.ts\" && tsc && type-coverage",
"build": "rimraf \"{lang/**,lib/**,script/**,test/**,}*.d.ts\" && tsc && type-coverage",
"generate": "node script/languages.js && node script/list.js && node script/fixtures.js",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node test/index.js",
Expand Down
98 changes: 50 additions & 48 deletions readme.md

Large diffs are not rendered by default.

45 changes: 18 additions & 27 deletions script/count.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,9 @@
* @typedef {import('mdast-zone').Handler} Handler
*/

import fs from 'fs'
import path from 'path'
import {zone} from 'mdast-zone'
import {u} from 'unist-builder'

/** @type {{[x: string]: unknown} & {dependencies: Object.<string, string>}} */
var pkg = JSON.parse(String(fs.readFileSync('package.json')))

/** @type {{languages: Object.<string, unknown>}} */
var components = JSON.parse(
String(
fs.readFileSync(path.join('node_modules', 'prismjs', 'components.json'))
)
)

var langs = Object.keys(components.languages).filter((d) => d !== 'meta').length
import {all, common} from './data.js'

export default function count() {
return transformer
Expand All @@ -35,21 +22,25 @@ function transformer(tree) {
function replace(start, _, end) {
return [
start,
u('paragraph', [
u('inlineCode', 'refractor'),
u('text', ' is built to work with all syntaxes supported by '),
u('linkReference', {identifier: 'Prism'}, [u('text', 'Prism')]),
u('text', ',\nthat’s '),
u('linkReference', {identifier: 'names'}, [
u('text', langs + ' languages')
u('list', {spread: false}, [
u('listItem', [
u('paragraph', [
u('inlineCode', 'lib/core.js'),
u('text', ' — 0 languages')
])
]),
u('text', ' (as of '),
u('linkReference', {identifier: 'prismjs'}, [
u('inlineCode', 'prism@' + pkg.dependencies.prismjs.slice(1))
u('listItem', [
u('paragraph', [
u('inlineCode', 'lib/common.js'),
u('text', ' (default) — ' + common.length + ' languages')
])
]),
u('text', ') and all\n'),
u('linkReference', {identifier: 'themes'}, [u('text', 'themes')]),
u('text', '.')
u('listItem', [
u('paragraph', [
u('inlineCode', 'lib/all.js'),
u('text', ' — ' + all.length + ' languages')
])
])
]),
end
]
Expand Down
57 changes: 57 additions & 0 deletions script/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import fs from 'fs'
import path from 'path'
import getLoader from 'prismjs/dependencies.js'

/** @type {{languages: Object.<string, unknown>}} */
var components = JSON.parse(
String(
fs.readFileSync(path.join('node_modules', 'prismjs', 'components.json'))
)
)

var allLanguages = Object.keys(components.languages).filter((d) => d !== 'meta')

/** @type {Array.<string>} */
export const all = getLoader(components, allLanguages).getIds()

/** @type {Array.<string>} */
export const common = getLoader(components, [
// These are alphabetical, but they are exported in registration order.
// They are based on the languages that lowlight exports as common,
// which is is turn based on what highlight.js treats as common.
// <https://github.com/wooorm/lowlight/blob/bf79fd1/script/build-registry.js#L39>
// <https://github.com/wooorm/lowlight/blob/main/lib/common.js>
'arduino',
'bash',
'clike',
'cpp',
'csharp',
'css',
'diff',
'go',
'ini',
'java',
'javascript',
'json',
'kotlin',
'less',
'lua',
'makefile',
'markdown',
'markup',
'objectivec',
'perl',
'php',
'python',
'r',
'regex',
'ruby',
'rust',
'sass',
'scss',
'sql',
'swift',
'typescript',
'vbnet',
'yaml'
]).getIds()
7 changes: 2 additions & 5 deletions script/languages.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import babel from '@babel/core'
import {detab} from 'detab'
import {trimLines} from 'trim-lines'
import alphaSort from 'alpha-sort'
import {all} from './data.js'
import {camelcase} from './camelcase.js'

/** @type {{languages: Object.<string, {require: string|string[], alias: string|string[]}>}} */
Expand All @@ -22,11 +23,7 @@ var components = JSON.parse(

var prefix = 'refractor-'

async.map(
Object.keys(components.languages).filter((d) => d !== 'meta'),
generate,
done
)
async.map(all, generate, done)

/**
* @param {Error} error
Expand Down
51 changes: 21 additions & 30 deletions script/list.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import fs from 'fs'
import path from 'path'
import {bail} from 'bail'
import chalk from 'chalk'
import getLoader from 'prismjs/dependencies.js'
import {camelcase} from './camelcase.js'
import {all, common} from './data.js'

/** @type {{languages: Object.<string, unknown>}} */
var components = JSON.parse(
String(
fs.readFileSync(path.join('node_modules', 'prismjs', 'components.json'))
)
fs.writeFileSync(path.join('lib', 'all.js'), generate(all))
fs.writeFileSync(path.join('lib', 'common.js'), generate(common))

console.log(
chalk.green('✓') + ' wrote `lib/all.js` for ' + all.length + ' languages'
)
console.log(
chalk.green('✓') +
' wrote `lib/common.js` for ' +
common.length +
' languages'
)
/** @type {Array.<string>} */
var names = getLoader(
components,
Object.keys(components.languages).filter((d) => d !== 'meta')
).getIds()

fs.writeFile(
'index.js',
[
/**
* @param {Array.<string>} list
*/
function generate(list) {
return [
'/**',
" * @typedef {import('./core.js').RefractorRoot} RefractorRoot",
" * @typedef {import('./core.js').RefractorElement} RefractorElement",
Expand All @@ -28,23 +30,12 @@ fs.writeFile(
" * @typedef {import('./core.js').Syntax} Syntax",
' */',
"import {refractor} from './core.js'",
...names.map(
(lang) => 'import ' + camelcase(lang) + " from './lang/" + lang + ".js'"
...list.map(
(lang) => 'import ' + camelcase(lang) + " from '../lang/" + lang + ".js'"
),
'',
'export {refractor}',
...names.map((lang) => 'refractor.register(' + camelcase(lang) + ')'),
...list.map((lang) => 'refractor.register(' + camelcase(lang) + ')'),
''
].join('\n'),
done
)

/**
* @param {Error} error
*/
function done(error) {
bail(error)
console.log(
chalk.green('✓') + ' wrote `index.js` for ' + names.length + ' languages'
)
].join('\n')
}
27 changes: 6 additions & 21 deletions script/support.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,12 @@
* @typedef {import('mdast-zone').Handler} Handler
*/

import fs from 'fs'
import path from 'path'
import {zone} from 'mdast-zone'
import {u} from 'unist-builder'
import alphaSort from 'alpha-sort'
import {all, common} from './data.js'

/** @type {{languages: Object.<string, unknown>}} */
var components = JSON.parse(
String(
fs.readFileSync(path.join('node_modules', 'prismjs', 'components.json'))
)
)

var itemPromises = Promise.all(
Object.keys(components.languages)
.filter((d) => d !== 'meta')
.sort(sort)
.map((d) => one(d))
)
var itemPromises = Promise.all(all.sort(sort).map((d) => one(d)))

export default function syntaxes() {
return transformer
Expand Down Expand Up @@ -73,16 +60,14 @@ async function one(name) {
}
}

return u('listItem', {checked: included(name + '.js')}, [
u('paragraph', content)
])
return u('listItem', {checked: included(name)}, [u('paragraph', content)])
}

/**
* @param {string} _
* @param {string} name
*/
function included(_) {
return false
function included(name) {
return common.includes(name)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @typedef {import('../core.js').Syntax} Syntax
* @typedef {import('../lib/core.js').Syntax} Syntax
* @typedef {import('hast').Node} Node
*/

Expand All @@ -9,7 +9,7 @@ import test from 'tape'
import rehype from 'rehype'
import {isHidden} from 'is-hidden'
import {removePosition} from 'unist-util-remove-position'
import {refractor} from '../index.js'
import {refractor} from '../lib/all.js'

test('.highlight(value, language)', function (t) {
t.throws(
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"include": ["core.js", "index.js", "script/**/*.js", "test/**/*.js"],
"include": ["index.js", "lib/**/*.js", "script/**/*.js", "test/**/*.js"],
"compilerOptions": {
"target": "ES2020",
"lib": ["ES2020"],
Expand Down

0 comments on commit b73ea4c

Please sign in to comment.