Skip to content

Commit

Permalink
Change to use export map
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Sep 13, 2023
1 parent 41b4be0 commit 1d0ef1d
Show file tree
Hide file tree
Showing 10 changed files with 711 additions and 674 deletions.
1,173 changes: 587 additions & 586 deletions lib/all.js

Large diffs are not rendered by default.

71 changes: 36 additions & 35 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,41 @@
* @typedef {import('./index.js').Grammar} Grammar
*/

import sourceC from '../lang/source.c.js'
import sourceCpp from '../lang/source.c++.js'
import sourceCPlatform from '../lang/source.c.platform.js'
import sourceCs from '../lang/source.cs.js'
import sourceCss from '../lang/source.css.js'
import sourceCssLess from '../lang/source.css.less.js'
import sourceCssScss from '../lang/source.css.scss.js'
import sourceDiff from '../lang/source.diff.js'
import sourceGo from '../lang/source.go.js'
import sourceGraphql from '../lang/source.graphql.js'
import sourceIni from '../lang/source.ini.js'
import sourceJava from '../lang/source.java.js'
import sourceJs from '../lang/source.js.js'
import sourceJson from '../lang/source.json.js'
import sourceKotlin from '../lang/source.kotlin.js'
import sourceLua from '../lang/source.lua.js'
import sourceMakefile from '../lang/source.makefile.js'
import sourceObjc from '../lang/source.objc.js'
import sourceObjcPlatform from '../lang/source.objc.platform.js'
import sourcePerl from '../lang/source.perl.js'
import sourcePython from '../lang/source.python.js'
import sourceR from '../lang/source.r.js'
import sourceRuby from '../lang/source.ruby.js'
import sourceRust from '../lang/source.rust.js'
import sourceShell from '../lang/source.shell.js'
import sourceSql from '../lang/source.sql.js'
import sourceSwift from '../lang/source.swift.js'
import sourceTs from '../lang/source.ts.js'
import sourceVbnet from '../lang/source.vbnet.js'
import sourceYaml from '../lang/source.yaml.js'
import textHtmlBasic from '../lang/text.html.basic.js'
import textHtmlPhp from '../lang/text.html.php.js'
import textMd from '../lang/text.md.js'
import textXml from '../lang/text.xml.js'
import textXmlSvg from '../lang/text.xml.svg.js'
import sourceC from '@wooorm/starry-night/source.c'
import sourceCpp from '@wooorm/starry-night/source.c++'
import sourceCPlatform from '@wooorm/starry-night/source.c.platform'
import sourceCs from '@wooorm/starry-night/source.cs'
import sourceCss from '@wooorm/starry-night/source.css'
import sourceCssLess from '@wooorm/starry-night/source.css.less'
import sourceCssScss from '@wooorm/starry-night/source.css.scss'
import sourceDiff from '@wooorm/starry-night/source.diff'
import sourceGo from '@wooorm/starry-night/source.go'
import sourceGraphql from '@wooorm/starry-night/source.graphql'
import sourceIni from '@wooorm/starry-night/source.ini'
import sourceJava from '@wooorm/starry-night/source.java'
import sourceJs from '@wooorm/starry-night/source.js'
import sourceJson from '@wooorm/starry-night/source.json'
import sourceKotlin from '@wooorm/starry-night/source.kotlin'
import sourceLua from '@wooorm/starry-night/source.lua'
import sourceMakefile from '@wooorm/starry-night/source.makefile'
import sourceObjc from '@wooorm/starry-night/source.objc'
import sourceObjcPlatform from '@wooorm/starry-night/source.objc.platform'
import sourcePerl from '@wooorm/starry-night/source.perl'
import sourcePython from '@wooorm/starry-night/source.python'
import sourceR from '@wooorm/starry-night/source.r'
import sourceRuby from '@wooorm/starry-night/source.ruby'
import sourceRust from '@wooorm/starry-night/source.rust'
import sourceShell from '@wooorm/starry-night/source.shell'
import sourceSql from '@wooorm/starry-night/source.sql'
import sourceSwift from '@wooorm/starry-night/source.swift'
import sourceTs from '@wooorm/starry-night/source.ts'
import sourceVbnet from '@wooorm/starry-night/source.vbnet'
import sourceYaml from '@wooorm/starry-night/source.yaml'
import textHtmlBasic from '@wooorm/starry-night/text.html.basic'
import textHtmlPhp from '@wooorm/starry-night/text.html.php'
import textMd from '@wooorm/starry-night/text.md'
import textXml from '@wooorm/starry-night/text.xml'
import textXmlSvg from '@wooorm/starry-night/text.xml.svg'

/** @type {ReadonlyArray<Grammar>} */
export const grammars = [
Expand All @@ -53,6 +53,7 @@ export const grammars = [
sourceIni,
sourceJava,
sourceJs,
// @ts-expect-error: TS is wrong, `.json` does not mean JSON.
sourceJson,
sourceKotlin,
sourceLua,
Expand Down
File renamed without changes.
File renamed without changes.
12 changes: 6 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@

import vscodeOniguruma from 'vscode-oniguruma'
import vscodeTextmate from 'vscode-textmate'
import {getOniguruma} from './get-oniguruma.js'
import {parse} from './parse.js'
import {theme} from './theme.js'
import {getOniguruma} from '#get-oniguruma'

/**
* Create a `StarryNight` that can highlight things with the given
Expand Down Expand Up @@ -210,7 +210,7 @@ export async function createStarryNight(grammars, options) {
* @example
* ```js
* import {createStarryNight} from '@wooorm/starry-night'
* import sourceCss from '@wooorm/starry-night/lang/source.css.js'
* import sourceCss from '@wooorm/starry-night/source.css'
*
* const starryNight = await createStarryNight([sourceCss])
*
Expand Down Expand Up @@ -272,8 +272,8 @@ export async function createStarryNight(grammars, options) {
* @example
* ```js
* import {createStarryNight} from '@wooorm/starry-night'
* import textXml from '@wooorm/starry-night/lang/text.xml.js'
* import textXmlSvg from '@wooorm/starry-night/lang/text.xml.svg.js'
* import textXml from '@wooorm/starry-night/text.xml'
* import textXmlSvg from '@wooorm/starry-night/text.xml.svg'
*
* const svg = await createStarryNight([textXmlSvg])
* console.log(svg.missingScopes()) //=> ['text.xml']
Expand Down Expand Up @@ -314,8 +314,8 @@ export async function createStarryNight(grammars, options) {
* @example
* ````js
* import {createStarryNight} from '@wooorm/starry-night'
* import sourceCss from '@wooorm/starry-night/lang/source.css.js'
* import textMd from '@wooorm/starry-night/lang/text.md.js'
* import sourceCss from '@wooorm/starry-night/source.css'
* import textMd from '@wooorm/starry-night/text.md'
* import {toHtml} from 'hast-util-to-html'
*
* const markdown = '```css\nem { color: red }\n```'
Expand Down
14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,16 @@
],
"type": "module",
"main": "index.js",
"browser": {
"./lib/get-oniguruma.js": "./lib/get-oniguruma.fetch.js"
"exports": {
".": "./index.js",
"./*": "./lang/*.js",
"./style/*": "./style/*.css"
},
"react-native": {
"./lib/get-oniguruma.js": "./lib/get-oniguruma.fetch.js"
"imports": {
"#get-oniguruma": {
"node": "./lib/get-oniguruma.fs.js",
"default": "./lib/get-oniguruma.default.js"
}
},
"types": "index.d.ts",
"files": [
Expand Down Expand Up @@ -145,6 +150,7 @@
"prettier": true,
"rules": {
"max-depth": "off",
"n/file-extension-in-import": "off",
"no-bitwise": "off",
"no-template-curly-in-string": "off",
"unicorn/prefer-at": "off",
Expand Down
56 changes: 33 additions & 23 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ To get the CSS in browsers, do (see [CSS][] for more info):

```html
<!-- This supports light and dark mode automatically. -->
<link rel="stylesheet" href="https://esm.sh/@wooorm/starry-night@2/style/both.css">
<link rel="stylesheet" href="https://esm.sh/@wooorm/starry-night@2/style/both">
```

## Use
Expand Down Expand Up @@ -204,9 +204,19 @@ Yields:
## API

This package exports the identifiers [`all`][api-all],
[`common`][api-common], and [`createStarryNight`][api-create-starry-night].
[`common`][api-common], and [`createStarryNight`][api-create-starry-night] from
the main module.
There is no default export.

It also includes grammars directly in its export map.
**Do not** use the `lang/` folder or the `.js` extension.
For CSS files, do use `style/` but don’t use `.css`:

```js
import mdx from '@wooorm/starry-night/source.mdx' // Grammar.
import tritanopiaDark from '@wooorm/starry-night/style/tritanopia-dark' // CSS.
```

### `all`

List of all grammars ([`Array<Grammar>`][api-grammar])
Expand Down Expand Up @@ -303,7 +313,7 @@ Node representing highlighted code ([`Root`][hast-root]).

```js
import {createStarryNight} from '@wooorm/starry-night'
import sourceCss from '@wooorm/starry-night/lang/source.css.js'
import sourceCss from '@wooorm/starry-night/source.css'

const starryNight = await createStarryNight([sourceCss])

Expand Down Expand Up @@ -342,8 +352,8 @@ List of grammar scopes, such as `'text.md'` (`Array<string>`).

```js
import {createStarryNight} from '@wooorm/starry-night'
import textXml from '@wooorm/starry-night/lang/text.xml.js'
import textXmlSvg from '@wooorm/starry-night/lang/text.xml.svg.js'
import textXml from '@wooorm/starry-night/text.xml'
import textXmlSvg from '@wooorm/starry-night/text.xml.svg'

const svg = await createStarryNight([textXmlSvg])
console.log(svg.missingScopes()) //=> ['text.xml']
Expand All @@ -369,8 +379,8 @@ Promise resolving to nothing (`Promise<undefined>`).

````js
import {createStarryNight} from '@wooorm/starry-night'
import sourceCss from '@wooorm/starry-night/lang/source.css.js'
import textMd from '@wooorm/starry-night/lang/text.md.js'
import sourceCss from '@wooorm/starry-night/source.css'
import textMd from '@wooorm/starry-night/text.md'
import {toHtml} from 'hast-util-to-html'

const markdown = '```css\nem { color: red }\n```'
Expand Down Expand Up @@ -564,7 +574,7 @@ for (const node of nodes) {
<!doctype html>
<meta charset=utf8>
<title>Hello</title>
<link rel=stylesheet href=https://esm.sh/@wooorm/starry-night@2/style/both.css>
<link rel=stylesheet href=https://esm.sh/@wooorm/starry-night@2/style/both>
<body>
<h1>Hello</h1>
<p>…world!</p>
Expand Down Expand Up @@ -997,21 +1007,21 @@ The shipped themes are as follows:

| name | Includes light scheme | Includes dark scheme |
| - | - | - |
| [`core.css`](style/core.css) | | |
| [`light.css`](style/light.css) || |
| [`dark.css`](style/dark.css) | ||
| [`both.css`](style/both.css) |||
| [`colorblind-light.css`](style/colorblind-light.css) || |
| [`colorblind-dark.css`](style/colorblind-dark.css) | ||
| [`colorblind.css`](style/colorblind.css) |||
| [`dimmed-dark.css`](style/dimmed-dark.css) | ||
| [`dimmed.css`](style/dimmed.css) |||
| [`high-contrast-light.css`](style/high-contrast-light.css) || |
| [`high-contrast-dark.css`](style/high-contrast-dark.css) | ||
| [`high-contrast.css`](style/high-contrast.css) |||
| [`tritanopia-light.css`](style/tritanopia-light.css) || |
| [`tritanopia-dark.css`](style/tritanopia-dark.css) | ||
| [`tritanopia.css`](style/tritanopia.css) |||
| [`@wooorm/starry-night/style/core`](style/core.css) | | |
| [`@wooorm/starry-night/style/light`](style/light.css) || |
| [`@wooorm/starry-night/style/dark`](style/dark.css) | ||
| [`@wooorm/starry-night/style/both`](style/both.css) |||
| [`@wooorm/starry-night/style/colorblind-light`](style/colorblind-light.css) || |
| [`@wooorm/starry-night/style/colorblind-dark`](style/colorblind-dark.css) | ||
| [`@wooorm/starry-night/style/colorblind`](style/colorblind.css) |||
| [`@wooorm/starry-night/style/dimmed-dark`](style/dimmed-dark.css) | ||
| [`@wooorm/starry-night/style/dimmed`](style/dimmed.css) |||
| [`@wooorm/starry-night/style/high-contrast-light`](style/high-contrast-light.css) || |
| [`@wooorm/starry-night/style/high-contrast-dark`](style/high-contrast-dark.css) | ||
| [`@wooorm/starry-night/style/high-contrast`](style/high-contrast.css) |||
| [`@wooorm/starry-night/style/tritanopia-light`](style/tritanopia-light.css) || |
| [`@wooorm/starry-night/style/tritanopia-dark`](style/tritanopia-dark.css) | ||
| [`@wooorm/starry-night/style/tritanopia`](style/tritanopia.css) |||

## Languages

Expand Down
19 changes: 16 additions & 3 deletions script/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,13 +549,26 @@ await Promise.all(
' */',
'',
...list.map(function (d) {
return 'import ' + scopeToId(d) + ' from "../lang/' + d + '.js"'
return (
'import ' +
scopeToId(d) +
' from "@wooorm/starry-night/' +
d +
'"'
)
}),
'',
'/** @type {ReadonlyArray<Grammar>} */',
'export const grammars = [',
...list.map(function (d) {
return ' ' + scopeToId(d) + ','
...list.flatMap(function (d) {
const useLine = ' ' + scopeToId(d) + ','

return d === 'source.json'
? [
' // @ts-expect-error: TS is wrong, `.json` does not mean JSON.',
useLine
]
: useLine
}),
']',
''
Expand Down
2 changes: 1 addition & 1 deletion script/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ ${generate(
)}
<p>…add the CSS:</p>
${generate(
`<link rel="stylesheet" href="https://esm.sh/@wooorm/starry-night@3/style/both.css">`,
`<link rel="stylesheet" href="https://esm.sh/@wooorm/starry-night@3/style/both">`,
'text.html.basic'
)}
<p>…then to use <code>starry-night</code>:</p>
Expand Down
38 changes: 22 additions & 16 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
/**
* @typedef {import('@wooorm/starry-night').Grammar} Grammar
*/

import assert from 'node:assert/strict'
import test from 'node:test'
import {toHtml} from 'hast-util-to-html'
import sourceAssembly from './lang/source.assembly.js'
import sourceCss from './lang/source.css.js'
import textPhp from './lang/text.html.php.js'
import textMd from './lang/text.md.js'
import textXml from './lang/text.xml.js'
import textXmlSvg from './lang/text.xml.svg.js'
import {common, createStarryNight} from './index.js'
import sourceAssembly from '@wooorm/starry-night/source.assembly'
import sourceCss from '@wooorm/starry-night/source.css'
import textPhp from '@wooorm/starry-night/text.html.php'
import textMd from '@wooorm/starry-night/text.md'
import textXml from '@wooorm/starry-night/text.xml'
import textXmlSvg from '@wooorm/starry-night/text.xml.svg'
import {common, createStarryNight} from '@wooorm/starry-night'

test('@wooorm/starry-night', async function (t) {
await t.test('should expose the public api', async function () {
assert.deepEqual(Object.keys(await import('./index.js')).sort(), [
assert.deepEqual(Object.keys(await import('@wooorm/starry-night')).sort(), [
'all',
'common',
'createStarryNight'
Expand Down Expand Up @@ -157,7 +161,7 @@ test('.flagToScope(flag)', async function (t) {
await t.test(
'should support language names with dots (`.`)',
async function () {
const {default: asn} = await import('./lang/source.asn.js')
const {default: asn} = await import('@wooorm/starry-night/source.asn')
const starryAsn = await createStarryNight([asn])

assert.equal(starryAsn.flagToScope('asn.1'), 'source.asn')
Expand All @@ -167,7 +171,7 @@ test('.flagToScope(flag)', async function (t) {
await t.test(
'should support language names with number signs (`#`)',
async function () {
const {default: cs} = await import('./lang/source.cs.js')
const {default: cs} = await import('@wooorm/starry-night/source.cs')
const starryCs = await createStarryNight([cs])

assert.equal(starryCs.flagToScope('c#'), 'source.cs')
Expand All @@ -177,7 +181,7 @@ test('.flagToScope(flag)', async function (t) {
await t.test(
'should support language names with plusses (`+`)',
async function () {
const {default: cpp} = await import('./lang/source.c++.js')
const {default: cpp} = await import('@wooorm/starry-night/source.c++')
const starryCpp = await createStarryNight([cpp])

assert.equal(starryCpp.flagToScope('c++'), 'source.c++')
Expand All @@ -187,7 +191,7 @@ test('.flagToScope(flag)', async function (t) {
await t.test(
'should support language names with asterisks (`*`)',
async function () {
const {default: fStar} = await import('./lang/source.fstar.js')
const {default: fStar} = await import('@wooorm/starry-night/source.fstar')
const starryFStar = await createStarryNight([fStar])

assert.equal(starryFStar.flagToScope('f*'), 'source.fstar')
Expand All @@ -197,7 +201,7 @@ test('.flagToScope(flag)', async function (t) {
await t.test(
"should support language names with apostrophes (`'`)",
async function () {
const {default: capnp} = await import('./lang/source.capnp.js')
const {default: capnp} = await import('@wooorm/starry-night/source.capnp')
const starryCapnp = await createStarryNight([capnp])

assert.equal(starryCapnp.flagToScope("cap'n-proto"), 'source.capnp')
Expand All @@ -207,7 +211,7 @@ test('.flagToScope(flag)', async function (t) {
await t.test(
'should support language names with parens (`(`, `)`)',
async function () {
const {default: dot} = await import('./lang/source.dot.js')
const {default: dot} = await import('@wooorm/starry-night/source.dot')
const starryDot = await createStarryNight([dot])

assert.equal(starryDot.flagToScope('graphviz-(dot)'), 'source.dot')
Expand All @@ -217,8 +221,10 @@ test('.flagToScope(flag)', async function (t) {
await t.test(
'should support language names with slashes (`/`)',
async function () {
const {default: json} = await import('./lang/source.json.js')
const starryJson = await createStarryNight([json])
const {default: json} = await import('@wooorm/starry-night/source.json')
// @ts-expect-error: TS is wrong, it doesn’t understand that `.json` is not an extension.
const grammar = /** @type {Grammar} */ (json)
const starryJson = await createStarryNight([grammar])

assert.equal(starryJson.flagToScope('max/msp'), 'source.json')
}
Expand Down

0 comments on commit 1d0ef1d

Please sign in to comment.