@quarto/jupyterlab-quarto 0.1.44 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE +1 -1
- package/README.md +48 -15
- package/lib/__tests__/jupyterlab_quarto.spec.d.ts +3 -0
- package/lib/__tests__/jupyterlab_quarto.spec.js +9 -0
- package/lib/ast/ast.d.ts +2 -0
- package/lib/ast/ast.js +40 -0
- package/lib/const.d.ts +4 -0
- package/lib/const.js +11 -0
- package/lib/hooks/codemirror.d.ts +5 -0
- package/lib/hooks/codemirror.js +77 -0
- package/lib/index.d.ts +5 -0
- package/lib/index.js +61 -0
- package/lib/manager.d.ts +8 -0
- package/lib/manager.js +115 -0
- package/lib/plugins/callouts.d.ts +2 -0
- package/lib/plugins/callouts.js +210 -0
- package/lib/plugins/cites.d.ts +2 -0
- package/lib/plugins/cites.js +166 -0
- package/lib/plugins/decorator.d.ts +2 -0
- package/lib/plugins/decorator.js +58 -0
- package/lib/plugins/divs.d.ts +5 -0
- package/lib/plugins/divs.js +111 -0
- package/lib/plugins/figure-divs.d.ts +2 -0
- package/lib/plugins/figure-divs.js +54 -0
- package/lib/plugins/figures.d.ts +16 -0
- package/lib/plugins/figures.js +98 -0
- package/lib/plugins/gridtables/common/gridtables/GetCells.d.ts +7 -0
- package/lib/plugins/gridtables/common/gridtables/GetCells.js +43 -0
- package/lib/plugins/gridtables/common/gridtables/GetColumnWidths.d.ts +7 -0
- package/lib/plugins/gridtables/common/gridtables/GetColumnWidths.js +22 -0
- package/lib/plugins/gridtables/common/markdown-it/ColumnAlignments.d.ts +7 -0
- package/lib/plugins/gridtables/common/markdown-it/ColumnAlignments.js +12 -0
- package/lib/plugins/gridtables/common/markdown-it/EmitTable.d.ts +4 -0
- package/lib/plugins/gridtables/common/markdown-it/EmitTable.js +64 -0
- package/lib/plugins/gridtables/common/markdown-it/GetCharCodeAtStartOfLine.d.ts +8 -0
- package/lib/plugins/gridtables/common/markdown-it/GetCharCodeAtStartOfLine.js +17 -0
- package/lib/plugins/gridtables/common/markdown-it/GetLine.d.ts +2 -0
- package/lib/plugins/gridtables/common/markdown-it/GetLine.js +9 -0
- package/lib/plugins/gridtables/common/markdown-it/ParseTable.d.ts +3 -0
- package/lib/plugins/gridtables/common/markdown-it/ParseTable.js +152 -0
- package/lib/plugins/gridtables/common/markdown-it/ParseTableResult.d.ts +12 -0
- package/lib/plugins/gridtables/common/markdown-it/ParseTableResult.js +17 -0
- package/lib/plugins/gridtables/index.d.ts +1 -0
- package/lib/plugins/gridtables/index.js +10 -0
- package/lib/plugins/gridtables/interfaces/markdown-it/IState.d.ts +10 -0
- package/lib/plugins/gridtables/interfaces/markdown-it/IState.js +5 -0
- package/lib/plugins/gridtables/interfaces/markdown-it/IToken.d.ts +6 -0
- package/lib/plugins/gridtables/interfaces/markdown-it/IToken.js +5 -0
- package/lib/plugins/gridtables/interfaces/markdown-it/TRuleFunction.d.ts +3 -0
- package/lib/plugins/gridtables/interfaces/markdown-it/TRuleFunction.js +5 -0
- package/lib/plugins/gridtables/rules/gridtable.d.ts +3 -0
- package/lib/plugins/gridtables/rules/gridtable.js +25 -0
- package/lib/plugins/index.d.ts +15 -0
- package/lib/plugins/index.js +21 -0
- package/lib/plugins/math.d.ts +6 -0
- package/lib/plugins/math.js +179 -0
- package/lib/plugins/mermaid/index.d.ts +4 -0
- package/lib/plugins/mermaid/index.js +60 -0
- package/lib/plugins/shortcodes.d.ts +3 -0
- package/lib/plugins/shortcodes.js +32 -0
- package/lib/plugins/spans.d.ts +2 -0
- package/lib/plugins/spans.js +37 -0
- package/lib/plugins/table-captions.d.ts +2 -0
- package/lib/plugins/table-captions.js +72 -0
- package/lib/plugins/utils/html.d.ts +11 -0
- package/lib/plugins/utils/html.js +50 -0
- package/lib/plugins/utils/markdownit.d.ts +15 -0
- package/lib/plugins/utils/markdownit.js +46 -0
- package/lib/plugins/utils/tok.d.ts +7 -0
- package/lib/plugins/utils/tok.js +13 -0
- package/lib/plugins/yaml.d.ts +2 -0
- package/lib/plugins/yaml.js +330 -0
- package/lib/providers/attrs.d.ts +1 -0
- package/lib/providers/attrs.js +15 -0
- package/lib/providers/callouts.d.ts +1 -0
- package/lib/providers/callouts.js +15 -0
- package/lib/providers/cites.d.ts +1 -0
- package/lib/providers/cites.js +15 -0
- package/lib/providers/decorator.d.ts +1 -0
- package/lib/providers/decorator.js +15 -0
- package/lib/providers/deflist.d.ts +1 -0
- package/lib/providers/deflist.js +15 -0
- package/lib/providers/divs.d.ts +1 -0
- package/lib/providers/divs.js +27 -0
- package/lib/providers/figure-divs.d.ts +1 -0
- package/lib/providers/figure-divs.js +15 -0
- package/lib/providers/figures.d.ts +1 -0
- package/lib/providers/figures.js +15 -0
- package/lib/providers/footnotes.d.ts +1 -0
- package/lib/providers/footnotes.js +15 -0
- package/lib/providers/gridtables.d.ts +1 -0
- package/lib/providers/gridtables.js +15 -0
- package/lib/providers/math.d.ts +1 -0
- package/lib/providers/math.js +104 -0
- package/lib/providers/mermaid.d.ts +1 -0
- package/lib/providers/mermaid.js +17 -0
- package/lib/providers/provider.d.ts +3 -0
- package/lib/providers/provider.js +12 -0
- package/lib/providers/shortcodes.d.ts +1 -0
- package/lib/providers/shortcodes.js +15 -0
- package/lib/providers/spans.d.ts +1 -0
- package/lib/providers/spans.js +15 -0
- package/lib/providers/sub.d.ts +1 -0
- package/lib/providers/sub.js +15 -0
- package/lib/providers/sup.d.ts +1 -0
- package/lib/providers/sup.js +15 -0
- package/lib/providers/table-captions.d.ts +1 -0
- package/lib/providers/table-captions.js +15 -0
- package/lib/providers/tasklists.d.ts +1 -0
- package/lib/providers/tasklists.js +15 -0
- package/lib/providers/yaml.d.ts +1 -0
- package/lib/providers/yaml.js +15 -0
- package/lib/types.d.ts +43 -0
- package/lib/types.js +1 -0
- package/lib/widgets.d.ts +14 -0
- package/lib/widgets.js +57 -0
- package/package.json +105 -39
- package/style/base.css +34 -33
- package/style/index.css +1 -1
- package/schema/plugin.json +0 -8
- package/src/@types/markdown-it-deflist.d.ts +0 -10
- package/src/@types/markdown-it-footnote.d.ts +0 -10
- package/src/@types/markdown-it-gridtables.d.ts +0 -10
- package/src/@types/markdown-it-implicit-figures.d.ts +0 -10
- package/src/@types/markdown-it-sub.d.ts +0 -10
- package/src/@types/markdown-it-sup.d.ts +0 -10
- package/src/@types/markdown-it-task-lists.d.ts +0 -10
package/LICENSE
CHANGED
package/README.md
CHANGED
@@ -1,23 +1,34 @@
|
|
1
1
|
# JupyterLab Quarto Extension
|
2
2
|
|
3
|
-
[Quarto](https://www.quarto.org) is an open source project that combines Jupyter notebooks with flexible options to use a single source document to produce high-quality articles, reports, presentations, websites, and books in HTML, PDF, MS Word, ePub, and more. Quarto supports a wide variety of useful new features useful in technical documents, including support for LaTeX equations, citations, cross-references, figure panels, callouts, advanced page layout, and more.
|
3
|
+
[Quarto](https://www.quarto.org) is an open source project that combines Jupyter notebooks with flexible options to use a single source document to produce high-quality articles, reports, presentations, websites, and books in HTML, PDF, MS Word, ePub, and more. Quarto supports a wide variety of useful new features useful in technical documents, including support for LaTeX equations, citations, cross-references, figure panels, callouts, advanced page layout, and more.
|
4
4
|
|
5
5
|
The JupyterLab Quarto extension allows JupyterLab to render notebooks which include Quarto markdown content.
|
6
6
|
<br/><br/>
|
7
|
+
|
7
8
|
<p align="center">
|
8
9
|
<img src="https://user-images.githubusercontent.com/261654/230087634-d5027ebc-8508-43b4-81c9-c4b7d6cfa738.png" width="60%">
|
9
10
|
</p>
|
10
11
|
|
12
|
+
### Binder
|
13
|
+
|
14
|
+
You can try an example of the extension in a notebook (though you can't actually render the notebook using Quarto) on Binder.
|
15
|
+
|
16
|
+
[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/quarto-dev/jupyterlab-quarto/main?urlpath=lab)
|
17
|
+
|
18
|
+
## Status
|
19
|
+
|
20
|
+
[![Github Actions Status](https://github.com/quarto-dev/jupyterlab-quarto/workflows/Build/badge.svg)](https://github.com/quarto-dev/jupyterlab-quarto/actions/workflows/build.yml)
|
21
|
+
|
11
22
|
## Requirements
|
12
23
|
|
13
|
-
- JupyterLab >=
|
24
|
+
- JupyterLab >= 4.0.0
|
14
25
|
|
15
26
|
## Install
|
16
27
|
|
17
28
|
To install the extension, execute:
|
18
29
|
|
19
30
|
```bash
|
20
|
-
pip install
|
31
|
+
pip install jupyterlab_quarto
|
21
32
|
```
|
22
33
|
|
23
34
|
## Uninstall
|
@@ -25,40 +36,42 @@ pip install jupyterlab-quarto
|
|
25
36
|
To remove the extension, execute:
|
26
37
|
|
27
38
|
```bash
|
28
|
-
pip uninstall
|
39
|
+
pip uninstall jupyterlab_quarto
|
29
40
|
```
|
30
41
|
|
31
42
|
## Contributing
|
32
43
|
|
33
44
|
### Development install
|
34
45
|
|
46
|
+
Note: You will need NodeJS to build the extension package.
|
47
|
+
|
48
|
+
The `jlpm` command is JupyterLab's pinned version of
|
49
|
+
[yarn](https://yarnpkg.com/) that is installed with JupyterLab. You may use
|
50
|
+
`yarn` or `npm` in lieu of `jlpm` below.
|
51
|
+
|
35
52
|
```bash
|
36
|
-
#
|
53
|
+
# Clone the repo to your local environment
|
54
|
+
# Change directory to the jupyterlab_quarto directory
|
37
55
|
# Install package in development mode
|
38
56
|
pip install -e "."
|
39
57
|
# Link your development version of the extension with JupyterLab
|
40
58
|
jupyter labextension develop . --overwrite
|
41
59
|
# Rebuild extension Typescript source after making changes
|
42
|
-
|
60
|
+
jlpm build
|
43
61
|
```
|
44
|
-
Note: You will need NodeJS to build the extension package.
|
45
|
-
|
46
|
-
Note: The `jlpm` command is JupyterLab's pinned version of
|
47
|
-
[yarn](https://yarnpkg.com/) that is installed with JupyterLab. You may use
|
48
|
-
`yarn` or `npm` in lieu of `jlpm` below.
|
49
62
|
|
50
63
|
You can watch the source directory and run JupyterLab at the same time in different terminals to watch for changes in the extension's source and automatically rebuild the extension.
|
51
64
|
|
52
65
|
```bash
|
53
66
|
# Watch the source directory in one terminal, automatically rebuilding when needed
|
54
|
-
|
67
|
+
jlpm watch
|
55
68
|
# Run JupyterLab in another terminal
|
56
69
|
jupyter lab
|
57
70
|
```
|
58
71
|
|
59
72
|
With the watch command running, every saved change will immediately be built locally and available in your running JupyterLab. Refresh JupyterLab to load the change in your browser (you may need to wait several seconds for the extension to be rebuilt).
|
60
73
|
|
61
|
-
By default, the `build` command generates the source maps for this extension to make it easier to debug using the browser dev tools. To also generate source maps for the JupyterLab core extensions, you can run the following command:
|
74
|
+
By default, the `jlpm build` command generates the source maps for this extension to make it easier to debug using the browser dev tools. To also generate source maps for the JupyterLab core extensions, you can run the following command:
|
62
75
|
|
63
76
|
```bash
|
64
77
|
jupyter lab build --minimize=False
|
@@ -67,12 +80,32 @@ jupyter lab build --minimize=False
|
|
67
80
|
### Development uninstall
|
68
81
|
|
69
82
|
```bash
|
70
|
-
pip uninstall
|
83
|
+
pip uninstall jupyterlab_quarto
|
71
84
|
```
|
72
85
|
|
73
86
|
In development mode, you will also need to remove the symlink created by `jupyter labextension develop`
|
74
87
|
command. To find its location, you can run `jupyter labextension list` to figure out where the `labextensions`
|
75
|
-
folder is located. Then you can remove the symlink named
|
88
|
+
folder is located. Then you can remove the symlink named `@quarto/jupyterlab-quarto` within that folder.
|
89
|
+
|
90
|
+
### Testing the extension
|
91
|
+
|
92
|
+
#### Frontend tests
|
93
|
+
|
94
|
+
This extension is using [Jest](https://jestjs.io/) for JavaScript code testing.
|
95
|
+
|
96
|
+
To execute them, execute:
|
97
|
+
|
98
|
+
```sh
|
99
|
+
jlpm
|
100
|
+
jlpm test
|
101
|
+
```
|
102
|
+
|
103
|
+
#### Integration tests
|
104
|
+
|
105
|
+
This extension uses [Playwright](https://playwright.dev/) for the integration tests (aka user level tests).
|
106
|
+
More precisely, the JupyterLab helper [Galata](https://github.com/jupyterlab/jupyterlab/tree/master/galata) is used to handle testing the extension in JupyterLab.
|
107
|
+
|
108
|
+
More information are provided within the [ui-tests](./ui-tests/README.md) README.
|
76
109
|
|
77
110
|
### Packaging the extension
|
78
111
|
|
package/lib/ast/ast.d.ts
ADDED
package/lib/ast/ast.js
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
export const toAst = (toks) => {
|
2
|
+
let pos = 0;
|
3
|
+
const inner = () => {
|
4
|
+
if (pos >= toks.length) {
|
5
|
+
return;
|
6
|
+
}
|
7
|
+
const tok = toks[pos];
|
8
|
+
if (tok.type.endsWith('_open')) {
|
9
|
+
++pos;
|
10
|
+
const children = [];
|
11
|
+
while (toks[pos].type !== tok.type.replace('_open', '_close')) {
|
12
|
+
children.push(inner());
|
13
|
+
}
|
14
|
+
++pos;
|
15
|
+
return {
|
16
|
+
...tok,
|
17
|
+
type: tok.type.replace('_open', ''),
|
18
|
+
children: children
|
19
|
+
};
|
20
|
+
}
|
21
|
+
if (tok.type === 'inline' && tok.children) {
|
22
|
+
const children = toAst(tok.children);
|
23
|
+
++pos;
|
24
|
+
return {
|
25
|
+
...tok,
|
26
|
+
children
|
27
|
+
};
|
28
|
+
}
|
29
|
+
++pos;
|
30
|
+
return tok;
|
31
|
+
};
|
32
|
+
const result = [];
|
33
|
+
while (pos < toks.length) {
|
34
|
+
const node = inner();
|
35
|
+
if (node) {
|
36
|
+
result.push(node);
|
37
|
+
}
|
38
|
+
}
|
39
|
+
return result;
|
40
|
+
};
|
package/lib/const.d.ts
ADDED
package/lib/const.js
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
/*
|
2
|
+
* const.ts
|
3
|
+
*
|
4
|
+
* Copyright (C) 2020-2023 Posit Software, PBC
|
5
|
+
*
|
6
|
+
*/
|
7
|
+
import { Token } from '@lumino/coreutils';
|
8
|
+
// The namespace for this project
|
9
|
+
export const kPackageNamespace = 'jupyterlab-quarto';
|
10
|
+
// The MarkdownIt manager token.
|
11
|
+
export const kMarkdownItMgr = new Token(kPackageNamespace);
|
@@ -0,0 +1,5 @@
|
|
1
|
+
import { Hook } from '../types';
|
2
|
+
import { IEditorLanguageRegistry } from '@jupyterlab/codemirror';
|
3
|
+
export declare function codeMirrorPreloadHook(registry: IEditorLanguageRegistry): Hook<string, string>;
|
4
|
+
export declare const codeMirrorHighlighter: (languageRegistry: IEditorLanguageRegistry) => (str: string, lang: string, _attr: any) => string;
|
5
|
+
export declare const codeMirrorHighlight: (str: string, lang: string, registry: IEditorLanguageRegistry) => string;
|
@@ -0,0 +1,77 @@
|
|
1
|
+
/*
|
2
|
+
* hooks.ts
|
3
|
+
*
|
4
|
+
* Copyright (C) 2020-2023 Posit Software, PBC
|
5
|
+
*
|
6
|
+
*/
|
7
|
+
export function codeMirrorPreloadHook(registry) {
|
8
|
+
// TODO: Properly deal with {r}, {{r}} style expressions
|
9
|
+
const fenced = new RegExp(/^`{3}([^\s]+)/g);
|
10
|
+
return {
|
11
|
+
run: async (source) => {
|
12
|
+
const newModes = new Map();
|
13
|
+
let match;
|
14
|
+
while ((match = fenced.exec(source))) {
|
15
|
+
if (!newModes.has(match[1])) {
|
16
|
+
newModes.set(match[1], registry.getLanguage(match[1]));
|
17
|
+
}
|
18
|
+
}
|
19
|
+
if (newModes.size) {
|
20
|
+
Promise.all(newModes.values()).catch(console.warn);
|
21
|
+
}
|
22
|
+
return source;
|
23
|
+
}
|
24
|
+
};
|
25
|
+
}
|
26
|
+
export const codeMirrorHighlighter = (languageRegistry) => {
|
27
|
+
return (str, lang, _attr) => {
|
28
|
+
if (!lang) {
|
29
|
+
return ''; // use external default escaping
|
30
|
+
}
|
31
|
+
try {
|
32
|
+
const spec = languageRegistry.findBest(lang);
|
33
|
+
if (!spec) {
|
34
|
+
console.warn(`No CodeMirror mode: ${lang}`);
|
35
|
+
return '';
|
36
|
+
}
|
37
|
+
const el = document.createElement('div');
|
38
|
+
try {
|
39
|
+
languageRegistry.highlight(str, spec, el);
|
40
|
+
return el.innerHTML;
|
41
|
+
}
|
42
|
+
catch (err) {
|
43
|
+
console.warn(`Failed to highlight ${lang} code`, err);
|
44
|
+
}
|
45
|
+
}
|
46
|
+
catch (err) {
|
47
|
+
console.warn(`No CodeMirror mode: ${lang}`);
|
48
|
+
console.warn(`Require CodeMirror mode error: ${err}`);
|
49
|
+
}
|
50
|
+
return '';
|
51
|
+
};
|
52
|
+
};
|
53
|
+
export const codeMirrorHighlight = (str, lang, registry) => {
|
54
|
+
if (!lang) {
|
55
|
+
return ''; // use external default escaping
|
56
|
+
}
|
57
|
+
try {
|
58
|
+
const spec = registry.findBest(lang);
|
59
|
+
if (!spec) {
|
60
|
+
console.warn(`No CodeMirror mode: ${lang}`);
|
61
|
+
return '';
|
62
|
+
}
|
63
|
+
const el = document.createElement('div');
|
64
|
+
try {
|
65
|
+
registry.highlight(str, spec, el);
|
66
|
+
return el.innerHTML;
|
67
|
+
}
|
68
|
+
catch (err) {
|
69
|
+
console.warn(`Failed to highlight ${lang} code`, err);
|
70
|
+
}
|
71
|
+
}
|
72
|
+
catch (err) {
|
73
|
+
console.warn(`No CodeMirror mode: ${lang}`);
|
74
|
+
console.warn(`Require CodeMirror mode error: ${err}`);
|
75
|
+
}
|
76
|
+
return '';
|
77
|
+
};
|
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
import { IEditorLanguageRegistry, IEditorThemeRegistry } from '@jupyterlab/codemirror';
|
2
|
+
import { mermaid } from './providers/mermaid';
|
3
|
+
import { kMarkdownItMgr, kPackageNamespace } from './const';
|
4
|
+
import { footnotes } from './providers/footnotes';
|
5
|
+
import '../style/index.css';
|
6
|
+
import { markdownItManager } from './manager';
|
7
|
+
import { divs } from './providers/divs';
|
8
|
+
import { deflist } from './providers/deflist';
|
9
|
+
import { gridtables } from './providers/gridtables';
|
10
|
+
import { sub } from './providers/sub';
|
11
|
+
import { sup } from './providers/sup';
|
12
|
+
import { tasklists } from './providers/tasklists';
|
13
|
+
import { cites } from './providers/cites';
|
14
|
+
import { attrs } from './providers/attrs';
|
15
|
+
import { callouts } from './providers/callouts';
|
16
|
+
import { decorator } from './providers/decorator';
|
17
|
+
import { yaml } from './providers/yaml';
|
18
|
+
import { math } from './providers/math';
|
19
|
+
import { figures } from './providers/figures';
|
20
|
+
import { figureDivs } from './providers/figure-divs';
|
21
|
+
import { tableCaptions } from './providers/table-captions';
|
22
|
+
import { spans } from './providers/spans';
|
23
|
+
import { shortcodes } from './providers/shortcodes';
|
24
|
+
const plugin = {
|
25
|
+
id: `${kPackageNamespace}:plugin`,
|
26
|
+
autoStart: true,
|
27
|
+
provides: kMarkdownItMgr,
|
28
|
+
requires: [IEditorThemeRegistry, IEditorLanguageRegistry],
|
29
|
+
activate: (_app, themeRegistry, languageRegistry) => {
|
30
|
+
console.log('JupyterLab extension @quarto/jupyterlab-quarto is activated!');
|
31
|
+
// Create a markdown rendering manager
|
32
|
+
return markdownItManager(themeRegistry, languageRegistry);
|
33
|
+
}
|
34
|
+
};
|
35
|
+
// Markdown It Extensions which provide base Pandoc behavior
|
36
|
+
const kPandocExtensions = [
|
37
|
+
footnotes,
|
38
|
+
spans,
|
39
|
+
attrs,
|
40
|
+
deflist,
|
41
|
+
figures,
|
42
|
+
gridtables,
|
43
|
+
sub,
|
44
|
+
sup,
|
45
|
+
tasklists,
|
46
|
+
divs,
|
47
|
+
math
|
48
|
+
];
|
49
|
+
// Markdown It Extensions which provide Quarto specific behavior
|
50
|
+
const kQuartoExtensions = [
|
51
|
+
figureDivs,
|
52
|
+
tableCaptions,
|
53
|
+
cites,
|
54
|
+
mermaid,
|
55
|
+
callouts,
|
56
|
+
decorator,
|
57
|
+
yaml,
|
58
|
+
shortcodes
|
59
|
+
];
|
60
|
+
// The extensions that should be enabled for Jupyter
|
61
|
+
export default [plugin, ...kPandocExtensions, ...kQuartoExtensions];
|
package/lib/manager.d.ts
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
import { IEditorLanguageRegistry, IEditorThemeRegistry } from '@jupyterlab/codemirror';
|
2
|
+
import { Options } from 'markdown-it';
|
3
|
+
import { RenderedMarkdown } from './widgets';
|
4
|
+
import { MarkdownItPluginProvider, Renderer } from './types';
|
5
|
+
export declare function markdownItManager(themeRegistry: IEditorThemeRegistry, languageRegistry: IEditorLanguageRegistry): {
|
6
|
+
registerPlugin(provider: MarkdownItPluginProvider): void;
|
7
|
+
getRenderer(widget: RenderedMarkdown, options?: Options): Promise<Renderer>;
|
8
|
+
};
|
package/lib/manager.js
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
/*
|
2
|
+
* manager.ts
|
3
|
+
*
|
4
|
+
* Copyright (C) 2020-2023 Posit Software, PBC
|
5
|
+
*
|
6
|
+
*/
|
7
|
+
import { markdownRendererFactory } from '@jupyterlab/rendermime';
|
8
|
+
import MarkdownIt from 'markdown-it';
|
9
|
+
import { RenderedMarkdown } from './widgets';
|
10
|
+
import { codeMirrorHighlighter, codeMirrorPreloadHook } from './hooks/codemirror';
|
11
|
+
// Provides resolved MarkdownIt options using the passed in options, the plugin
|
12
|
+
// options, and default options.
|
13
|
+
const resolveOptions = (widget, options, providers, themeRegistry, languageRegistry) => {
|
14
|
+
// Build options table
|
15
|
+
let allOptions = {
|
16
|
+
html: true,
|
17
|
+
linkify: true,
|
18
|
+
typographer: true,
|
19
|
+
langPrefix: `cm-s-${themeRegistry.defaultTheme} language-`,
|
20
|
+
highlight: codeMirrorHighlighter(languageRegistry)
|
21
|
+
};
|
22
|
+
for (const plugin of providers) {
|
23
|
+
if (plugin.options) {
|
24
|
+
try {
|
25
|
+
// Add options for this plugin
|
26
|
+
allOptions = { ...allOptions, ...plugin.options(widget) };
|
27
|
+
}
|
28
|
+
catch (err) {
|
29
|
+
console.warn(`Failed to get options from markdown-it plugin ${plugin.id}`, err);
|
30
|
+
}
|
31
|
+
}
|
32
|
+
}
|
33
|
+
return {
|
34
|
+
...allOptions,
|
35
|
+
...options
|
36
|
+
};
|
37
|
+
};
|
38
|
+
export function markdownItManager(themeRegistry, languageRegistry) {
|
39
|
+
// The plugin providers
|
40
|
+
const pluginProviders = new Map();
|
41
|
+
// The IMarkdownItManager
|
42
|
+
const manager = {
|
43
|
+
registerPlugin(provider) {
|
44
|
+
pluginProviders.set(provider.id, provider);
|
45
|
+
},
|
46
|
+
async getRenderer(widget, options = {}) {
|
47
|
+
var _a, _b, _c, _d;
|
48
|
+
// Fetch the list of providers
|
49
|
+
const providers = [...pluginProviders.values()];
|
50
|
+
providers.sort(sortRanked);
|
51
|
+
// Create MarkdownIt instance
|
52
|
+
const allOptions = resolveOptions(widget, options, providers, themeRegistry, languageRegistry);
|
53
|
+
let md = new MarkdownIt('default', allOptions);
|
54
|
+
// Lifecycle hooks
|
55
|
+
const preParseHooks = [];
|
56
|
+
const postRenderHooks = [];
|
57
|
+
// add mode pre-loading hook if using default highlighter
|
58
|
+
if (codeMirrorHighlighter(languageRegistry) === allOptions.highlight) {
|
59
|
+
preParseHooks.push(codeMirrorPreloadHook(languageRegistry));
|
60
|
+
}
|
61
|
+
// Build MarkdownIt and load lifecycle hooks
|
62
|
+
for (const provider of providers) {
|
63
|
+
try {
|
64
|
+
// Load MarkdownIt plugin
|
65
|
+
const [plugin, ...pluginOptions] = await provider.plugin();
|
66
|
+
// Build MarkdownIt instance
|
67
|
+
md = md.use(plugin, ...pluginOptions);
|
68
|
+
// Build table of lifecycle hooks
|
69
|
+
if (((_a = provider.hooks) === null || _a === void 0 ? void 0 : _a.preParse) !== undefined) {
|
70
|
+
preParseHooks.push((_b = provider.hooks) === null || _b === void 0 ? void 0 : _b.preParse);
|
71
|
+
}
|
72
|
+
if (((_c = provider.hooks) === null || _c === void 0 ? void 0 : _c.postRender) !== undefined) {
|
73
|
+
postRenderHooks.push((_d = provider.hooks) === null || _d === void 0 ? void 0 : _d.postRender);
|
74
|
+
}
|
75
|
+
}
|
76
|
+
catch (err) {
|
77
|
+
console.warn(`Failed to load/use markdown-it plugin ${provider.id}`, err);
|
78
|
+
}
|
79
|
+
}
|
80
|
+
// Sort hooks by rank
|
81
|
+
preParseHooks.sort(sortRanked);
|
82
|
+
postRenderHooks.sort(sortRanked);
|
83
|
+
return {
|
84
|
+
// Parse and render Markdown
|
85
|
+
render: content => {
|
86
|
+
return md.render(content);
|
87
|
+
},
|
88
|
+
// Run hooks serially
|
89
|
+
preParse: async (content) => {
|
90
|
+
for (const hook of preParseHooks) {
|
91
|
+
content = await hook.run(content);
|
92
|
+
}
|
93
|
+
return content;
|
94
|
+
},
|
95
|
+
// Run hooks serially
|
96
|
+
postRender: async (node) => {
|
97
|
+
for (const hook of postRenderHooks) {
|
98
|
+
await hook.run(node);
|
99
|
+
}
|
100
|
+
}
|
101
|
+
};
|
102
|
+
}
|
103
|
+
};
|
104
|
+
// Register the Renderer
|
105
|
+
markdownRendererFactory.createRenderer = (options) => {
|
106
|
+
return new RenderedMarkdown(options, manager);
|
107
|
+
};
|
108
|
+
return manager;
|
109
|
+
}
|
110
|
+
// Sorts by rank, using 100 if no default is provided.
|
111
|
+
const kDefaultRank = 100;
|
112
|
+
const sortRanked = (left, right) => {
|
113
|
+
var _a, _b;
|
114
|
+
return ((_a = left.rank) !== null && _a !== void 0 ? _a : kDefaultRank) - ((_b = right.rank) !== null && _b !== void 0 ? _b : kDefaultRank);
|
115
|
+
};
|