@jupyterlab/toc 6.7.0-beta.0 → 6.7.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/utils/markdown.d.ts +3 -1
- package/lib/utils/markdown.js +8 -4
- package/lib/utils/markdown.js.map +1 -1
- package/package.json +10 -10
- package/src/utils/markdown.ts +16 -4
package/lib/utils/markdown.d.ts
CHANGED
|
@@ -27,13 +27,15 @@ export declare function getHeadingId(markdownParser: IMarkdownParser, raw: strin
|
|
|
27
27
|
*
|
|
28
28
|
* @param text - Markdown text
|
|
29
29
|
* @param parser - A Markdown parser instance used to render the content to HTML.
|
|
30
|
+
* @param mathParseOptions - How math is found in the text, as for the
|
|
31
|
+
* typesetter that renders it; the default delimiters when omitted.
|
|
30
32
|
* @returns List of headings
|
|
31
33
|
*
|
|
32
34
|
* @remarks
|
|
33
35
|
* Renders Markdown to HTML and extracts `<h1>`–`<h6>` elements.
|
|
34
36
|
* Returns an empty list if the parser is `null`.
|
|
35
37
|
*/
|
|
36
|
-
export declare function parseHeadings(markdownText: string, parser: IMarkdownParser | null): Promise<IMarkdownHeading[]>;
|
|
38
|
+
export declare function parseHeadings(markdownText: string, parser: IMarkdownParser | null, mathParseOptions?: IRenderMime.ILatexTypesetter.IMathParseOptions): Promise<IMarkdownHeading[]>;
|
|
37
39
|
/**
|
|
38
40
|
* @deprecated in favour of async parseHeadings()
|
|
39
41
|
*
|
package/lib/utils/markdown.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Copyright (c) Jupyter Development Team.
|
|
2
2
|
// Distributed under the terms of the Modified BSD License.
|
|
3
3
|
import { Sanitizer } from '@jupyterlab/apputils';
|
|
4
|
-
import { renderMarkdown } from '@jupyterlab/rendermime';
|
|
4
|
+
import { removeMath, renderMarkdown, replaceMath } from '@jupyterlab/rendermime';
|
|
5
5
|
/**
|
|
6
6
|
* Build the heading html id.
|
|
7
7
|
*
|
|
@@ -41,23 +41,27 @@ export async function getHeadingId(markdownParser, raw, level, sanitizer) {
|
|
|
41
41
|
*
|
|
42
42
|
* @param text - Markdown text
|
|
43
43
|
* @param parser - A Markdown parser instance used to render the content to HTML.
|
|
44
|
+
* @param mathParseOptions - How math is found in the text, as for the
|
|
45
|
+
* typesetter that renders it; the default delimiters when omitted.
|
|
44
46
|
* @returns List of headings
|
|
45
47
|
*
|
|
46
48
|
* @remarks
|
|
47
49
|
* Renders Markdown to HTML and extracts `<h1>`–`<h6>` elements.
|
|
48
50
|
* Returns an empty list if the parser is `null`.
|
|
49
51
|
*/
|
|
50
|
-
export async function parseHeadings(markdownText, parser) {
|
|
52
|
+
export async function parseHeadings(markdownText, parser, mathParseOptions) {
|
|
51
53
|
if (!parser || !parser.getHeadingTokens) {
|
|
52
54
|
console.warn('Unable to parse headings; Markdown parser is missing or does not implement getHeadingTokens().');
|
|
53
55
|
return [];
|
|
54
56
|
}
|
|
55
57
|
// Get the heading tokens from the parser, including the source line metadata
|
|
56
|
-
const headingTokens = await parser.getHeadingTokens(markdownText);
|
|
58
|
+
const headingTokens = await parser.getHeadingTokens(markdownText, mathParseOptions);
|
|
57
59
|
// Convert each heading token into HTML and extract the heading elements,
|
|
58
60
|
// as this provides a clean and reliable way to retrieve the headings.
|
|
59
61
|
const parseHeadings = await Promise.all(headingTokens.map(async (token) => {
|
|
60
|
-
|
|
62
|
+
// Hide math from the parser, as `renderMarkdown` does.
|
|
63
|
+
const { text, math } = removeMath(token.raw, mathParseOptions);
|
|
64
|
+
const renderedHtml = replaceMath(await parser.render(text), math);
|
|
61
65
|
const dom = new DOMParser().parseFromString(renderedHtml, 'text/html');
|
|
62
66
|
const htmlHeadings = Array.from(dom.querySelectorAll('h1, h2, h3, h4, h5, h6'));
|
|
63
67
|
return htmlHeadings.map(e => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"markdown.js","sourceRoot":"","sources":["../../src/utils/markdown.ts"],"names":[],"mappings":"AAAA,0CAA0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C;gBAChC,IAAI,EAAE,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC;aAC7B,CAAC;QACJ,CAAC;IACH,CAAC;IACD,0GAA0G;IAC1G,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,0BAA0B,CAAC,CAAC,CAAC,0DAA0D;IAC1G,IAAI,KAAK,EAAE,CAAC;QACV,OAAO;YACL,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC;YACd,KAAK,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC;YAC7B,IAAI,EAAE,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YAChC,GAAG,EAAE,IAAI;SACV,CAAC;IACJ,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAED,SAAS,UAAU,CAAC,OAAe;IACjC,0DAA0D;IAC1D,OAAO,OAAO,CAAC,OAAO,CAAC,iBAAiB,EAAE,IAAI,CAAC,CAAC;AAClD,CAAC;AAED;;GAEG;AACH,MAAM,WAAW,GACf,kFAAkF,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jupyterlab/toc",
|
|
3
|
-
"version": "6.7.0-beta.
|
|
3
|
+
"version": "6.7.0-beta.1",
|
|
4
4
|
"description": "JupyterLab - Table of Contents widget",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"jupyterlab"
|
|
@@ -41,14 +41,14 @@
|
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"@jupyter/react-components": "^0.17.1",
|
|
44
|
-
"@jupyterlab/apputils": "^4.8.0-beta.
|
|
45
|
-
"@jupyterlab/coreutils": "^6.7.0-beta.
|
|
46
|
-
"@jupyterlab/docregistry": "^4.7.0-beta.
|
|
47
|
-
"@jupyterlab/observables": "^5.7.0-beta.
|
|
48
|
-
"@jupyterlab/rendermime": "^4.7.0-beta.
|
|
49
|
-
"@jupyterlab/rendermime-interfaces": "^3.15.0-beta.
|
|
50
|
-
"@jupyterlab/translation": "^4.7.0-beta.
|
|
51
|
-
"@jupyterlab/ui-components": "^4.7.0-beta.
|
|
44
|
+
"@jupyterlab/apputils": "^4.8.0-beta.1",
|
|
45
|
+
"@jupyterlab/coreutils": "^6.7.0-beta.1",
|
|
46
|
+
"@jupyterlab/docregistry": "^4.7.0-beta.1",
|
|
47
|
+
"@jupyterlab/observables": "^5.7.0-beta.1",
|
|
48
|
+
"@jupyterlab/rendermime": "^4.7.0-beta.1",
|
|
49
|
+
"@jupyterlab/rendermime-interfaces": "^3.15.0-beta.1",
|
|
50
|
+
"@jupyterlab/translation": "^4.7.0-beta.1",
|
|
51
|
+
"@jupyterlab/ui-components": "^4.7.0-beta.1",
|
|
52
52
|
"@lumino/coreutils": "^2.2.3",
|
|
53
53
|
"@lumino/disposable": "^2.1.6",
|
|
54
54
|
"@lumino/messaging": "^2.0.5",
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
"react": "^18.2.0"
|
|
58
58
|
},
|
|
59
59
|
"devDependencies": {
|
|
60
|
-
"@jupyterlab/testing": "^4.7.0-beta.
|
|
60
|
+
"@jupyterlab/testing": "^4.7.0-beta.1",
|
|
61
61
|
"@types/jest": "^29.2.0",
|
|
62
62
|
"@types/react": "^18.0.26",
|
|
63
63
|
"jest": "^29.2.0",
|
package/src/utils/markdown.ts
CHANGED
|
@@ -3,7 +3,11 @@
|
|
|
3
3
|
|
|
4
4
|
import { Sanitizer } from '@jupyterlab/apputils';
|
|
5
5
|
import type { IMarkdownParser } from '@jupyterlab/rendermime';
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
removeMath,
|
|
8
|
+
renderMarkdown,
|
|
9
|
+
replaceMath
|
|
10
|
+
} from '@jupyterlab/rendermime';
|
|
7
11
|
import type { IRenderMime } from '@jupyterlab/rendermime-interfaces';
|
|
8
12
|
import type { TableOfContents } from '../tokens';
|
|
9
13
|
|
|
@@ -69,6 +73,8 @@ export async function getHeadingId(
|
|
|
69
73
|
*
|
|
70
74
|
* @param text - Markdown text
|
|
71
75
|
* @param parser - A Markdown parser instance used to render the content to HTML.
|
|
76
|
+
* @param mathParseOptions - How math is found in the text, as for the
|
|
77
|
+
* typesetter that renders it; the default delimiters when omitted.
|
|
72
78
|
* @returns List of headings
|
|
73
79
|
*
|
|
74
80
|
* @remarks
|
|
@@ -77,7 +83,8 @@ export async function getHeadingId(
|
|
|
77
83
|
*/
|
|
78
84
|
export async function parseHeadings(
|
|
79
85
|
markdownText: string,
|
|
80
|
-
parser: IMarkdownParser | null
|
|
86
|
+
parser: IMarkdownParser | null,
|
|
87
|
+
mathParseOptions?: IRenderMime.ILatexTypesetter.IMathParseOptions
|
|
81
88
|
): Promise<IMarkdownHeading[]> {
|
|
82
89
|
if (!parser || !parser.getHeadingTokens) {
|
|
83
90
|
console.warn(
|
|
@@ -87,13 +94,18 @@ export async function parseHeadings(
|
|
|
87
94
|
}
|
|
88
95
|
|
|
89
96
|
// Get the heading tokens from the parser, including the source line metadata
|
|
90
|
-
const headingTokens = await parser.getHeadingTokens(
|
|
97
|
+
const headingTokens = await parser.getHeadingTokens(
|
|
98
|
+
markdownText,
|
|
99
|
+
mathParseOptions
|
|
100
|
+
);
|
|
91
101
|
|
|
92
102
|
// Convert each heading token into HTML and extract the heading elements,
|
|
93
103
|
// as this provides a clean and reliable way to retrieve the headings.
|
|
94
104
|
const parseHeadings = await Promise.all(
|
|
95
105
|
headingTokens.map(async token => {
|
|
96
|
-
|
|
106
|
+
// Hide math from the parser, as `renderMarkdown` does.
|
|
107
|
+
const { text, math } = removeMath(token.raw, mathParseOptions);
|
|
108
|
+
const renderedHtml = replaceMath(await parser.render(text), math);
|
|
97
109
|
const dom = new DOMParser().parseFromString(renderedHtml, 'text/html');
|
|
98
110
|
const htmlHeadings = Array.from(
|
|
99
111
|
dom.querySelectorAll('h1, h2, h3, h4, h5, h6')
|