Skip to content

Commit b960519

Browse files
authored
feat: output type definitions, and allow new Docsify(opts) to accept options for ESM usage (#2392)
This `new Docsify(opts)` API can be safely converted to a Custom Element constructor with backwards compatibility later.
1 parent de63e3a commit b960519

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+3490
-322
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ jobs:
4949
run: npm run test:unit -- --ci --runInBand
5050
- name: Integration Tests
5151
run: npm run test:integration -- --ci --runInBand
52+
- name: Consumption Tests
53+
run: npm run test:consume-types
5254

5355
test-playwright:
5456
runs-on: ubuntu-latest

.gitignore

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
.vercel
55
_playwright-report
66
_playwright-results
7-
dist
8-
lib
97
node_modules
108

119
# Files
@@ -14,3 +12,10 @@ node_modules
1412

1513
# Exceptions
1614
!.gitkeep
15+
16+
# Output folder for the global build only
17+
dist
18+
19+
# TypeScript declaration files for standard ESM consumption
20+
src/**/*.d.ts
21+
src/**/*.d.ts.map

.prettierignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ _vars.css
88
_vars-advanced.css
99
CHANGELOG.md
1010
emoji-data.*
11-
HISTORY.md
11+
HISTORY.md

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"editor.defaultFormatter": "esbenp.prettier-vscode",
3-
"cSpell.words": ["coverpage"]
3+
"cSpell.words": ["coverpage"],
4+
"typescript.tsdk": "node_modules/typescript/lib"
45
}

docs/configuration.md

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ window.$docsify = {
456456

457457
## name
458458

459-
- Type: `String`
459+
- Type: `Boolean | String`
460460

461461
Website name as it appears in the sidebar.
462462

@@ -474,6 +474,22 @@ window.$docsify = {
474474
};
475475
```
476476

477+
If `true`, the website name will be inferred from the document's `<title>` tag.
478+
479+
```js
480+
window.$docsify = {
481+
name: true,
482+
};
483+
```
484+
485+
If `false` or empty, no name will be displayed.
486+
487+
```js
488+
window.$docsify = {
489+
name: false,
490+
};
491+
```
492+
477493
## nameLink
478494

479495
- Type: `String`
@@ -652,6 +668,10 @@ window.$docsify = {
652668
};
653669
```
654670

671+
## plugins
672+
673+
See [Plugins](./plugins.md).
674+
655675
## relativePath
656676

657677
- Type: `Boolean`
@@ -706,6 +726,8 @@ window.$docsify = {
706726
};
707727
```
708728

729+
If undefined or empty, no GitHub corner will be displayed.
730+
709731
## requestHeaders
710732

711733
- Type: `Object`
@@ -887,9 +909,9 @@ window.$docsify = {
887909
Determines if/how the site's [skip navigation link](https://webaim.org/techniques/skipnav/) will be rendered.
888910

889911
```js
890-
// Render skip link for all routes (default)
912+
// Render skip link for all routes
891913
window.$docsify = {
892-
skipLink: 'Skip to main content',
914+
skipLink: 'Skip to content',
893915
};
894916
```
895917

@@ -912,6 +934,13 @@ window.$docsify = {
912934
};
913935
```
914936

937+
```js
938+
// Use default
939+
window.$docsify = {
940+
skipLink: true, // "Skip to main content"
941+
};
942+
```
943+
915944
## subMaxLevel
916945

917946
- Type: `Number`

docs/plugins.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# List of Plugins
22

3+
These are built-in and external plugins for Docsify.
4+
5+
See also how to [Write a Plugin](./write-a-plugin.md).
6+
37
## Full text search
48

59
By default, the hyperlink on the current page is recognized and the content is saved in `IndexedDB`. You can also specify the path to the files.
@@ -25,7 +29,7 @@ By default, the hyperlink on the current page is recognized and the content is s
2529
insertBefore: '.sidebar-nav', // CSS selector in .sidebar scope
2630
2731
maxAge: 86400000, // Expiration time, the default one day
28-
paths: [], // or 'auto'
32+
paths: [], // string[] of files to search in, or 'auto' for discovery based on your sidebar
2933
placeholder: 'Type to search',
3034
3135
// Localization

package-lock.json

Lines changed: 21 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,22 @@
2424
},
2525
"type": "module",
2626
"main": "dist/docsify.js",
27+
"types": "src/core/Docsify.d.ts",
2728
"exports": {
2829
".": "./src/core/Docsify.js",
2930
"./*": "./*"
3031
},
3132
"files": [
32-
"dist"
33+
"dist",
34+
"src"
3335
],
3436
"lint-staged": {
3537
"*.js": "eslint --fix"
3638
},
3739
"dependencies": {
40+
"common-tags": "^1.8.0",
3841
"dexie": "^4.0.11",
42+
"marked": "^16.0.0",
3943
"medium-zoom": "^1.1.0",
4044
"opencollective-postinstall": "^2.0.2",
4145
"prismjs": "^1.29.0",
@@ -51,10 +55,11 @@
5155
"@rollup/plugin-node-resolve": "^16.0.0",
5256
"@rollup/plugin-replace": "^6.0.1",
5357
"@rollup/plugin-terser": "^0.4.3",
58+
"@types/common-tags": "^1.8.4",
5459
"@types/eslint": "^8.40.2",
60+
"@types/prismjs": "^1.26.5",
5561
"axios": "^1.5.0",
5662
"browser-sync": "^3.0.2",
57-
"common-tags": "^1.8.0",
5863
"conventional-changelog-cli": "^3.0.0",
5964
"cross-env": "^10.0.0",
6065
"cssnano": "^7.0.1",
@@ -69,7 +74,6 @@
6974
"jest": "^30.0.4",
7075
"jest-environment-jsdom": "^30.0.5",
7176
"lint-staged": "^16.1.0",
72-
"marked": "^16.0.0",
7377
"npm-run-all": "^4.1.5",
7478
"postcss-cli": "^11.0.0",
7579
"postcss-import": "^16.1.0",
@@ -78,6 +82,7 @@
7882
"rimraf": "^6.1.0",
7983
"rollup": "^4.17.2",
8084
"rollup-plugin-import-css": "^4.0.1",
85+
"typescript": "^5.9.3",
8186
"vue": "^3.4.27",
8287
"xhr-mock": "^2.5.1"
8388
},
@@ -87,8 +92,9 @@
8792
"build:css:min": "cross-env NODE_ENV='production' npm run build:css -- --ext .min.css",
8893
"build:emoji": "node ./build/emoji.js",
8994
"build:js": "rollup -c",
90-
"build": "run-s clean build:js build:css build:css:min build:cover",
91-
"clean": "rimraf --glob dist/** themes/** _playwright*/**",
95+
"build:types": "tsc",
96+
"build": "run-s clean build:types build:js build:css build:css:min build:cover",
97+
"clean": "rimraf --glob \"dist/**\" \"themes/**\" \"_playwright*/**\" \"src/**/*.d.ts\" \"src/**/*.d.ts.map\"",
9298
"dev": "run-p serve:dev watch:*",
9399
"docker:build:test": "npm run docker:cli -- build:test",
94100
"docker:build": "docker build -f Dockerfile -t docsify-test:local .",
@@ -111,11 +117,15 @@
111117
"test:e2e": "playwright test",
112118
"test:e2e:chromium": "playwright test --project='chromium'",
113119
"test:e2e:ui": "playwright test --ui",
120+
"test:e2e:consume-types": "echo TODO: test the consume-types example with ESM modules",
114121
"test:integration": "npm run test:jest -- --selectProjects integration",
115122
"test:jest": "cross-env NODE_OPTIONS=--experimental-vm-modules jest",
116123
"test:unit": "npm run test:jest -- --selectProjects unit",
117124
"test:update:snapshot": "npm run test:jest -- --updateSnapshot",
118-
"test": "run-s test:jest test:e2e",
125+
"test:consume-types": "cd test/consume-types && npm clean-install --install-links && npm run typecheck",
126+
"test": "run-s test:jest test:e2e test:consume-types",
127+
"typecheck": "tsc --noEmit",
128+
"typecheck:watch": "tsc --noEmit --watch",
119129
"watch:css": "run-p 'build:css -- --watch' 'build:css:min -- --watch'",
120130
"watch:js": "npm run build:js -- --watch"
121131
}

src/core/Docsify.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import prism from 'prismjs';
12
import { Router } from './router/index.js';
23
import { Render } from './render/index.js';
34
import { Fetch } from './fetch/index.js';
@@ -8,16 +9,29 @@ import config from './config.js';
89
import { isFn } from './util/core.js';
910
import { Lifecycle } from './init/lifecycle.js';
1011

12+
export { prism };
13+
export { marked } from 'marked';
14+
export * as util from './util/index.js';
15+
export * as dom from './util/dom.js';
16+
export { Compiler } from './render/compiler.js';
17+
export { slugify } from './render/slugify.js';
18+
export { get } from './util/ajax.js';
19+
1120
/** @typedef {new (...args: any[]) => any} Constructor */
21+
/** @typedef {import('./config.js').DocsifyConfig} DocsifyConfig */
1222

1323
export class Docsify extends Fetch(
1424
Events(Render(VirtualRoutes(Router(Lifecycle(Object))))),
1525
) {
16-
config = config(this);
26+
/** @type {DocsifyConfig} */
27+
config;
1728

18-
constructor() {
29+
/** @param {Partial<DocsifyConfig>} conf */
30+
constructor(conf = {}) {
1931
super();
2032

33+
this.config = config(this, conf);
34+
2135
this.initLifecycle(); // Init hooks
2236
this.initPlugin(); // Install plugins
2337
this.callHook('init');
@@ -45,3 +59,5 @@ export class Docsify extends Fetch(
4559
});
4660
}
4761
}
62+
63+
export const version = '__VERSION__';

0 commit comments

Comments
 (0)