@limetech/lime-elements 40.20.0 → 40.20.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/CHANGELOG.md +12 -0
- package/dist/cjs/limel-chart.cjs.entry.js +29 -18
- package/dist/cjs/limel-markdown.cjs.entry.js +2 -2
- package/dist/cjs/limel-prosemirror-adapter.cjs.entry.js +1 -1
- package/dist/cjs/{markdown-exporter-BWS2EBxq.js → markdown-exporter-CGNfjGr9.js} +80 -897
- package/dist/cjs/{markdown-parser-toAp9-Eq.js → markdown-parser-C14XF_G3.js} +994 -38
- package/dist/collection/components/chart/chart.css +20 -5
- package/dist/collection/components/chart/chart.js +3 -0
- package/dist/collection/components/chart/options.js +12 -16
- package/dist/collection/components/chart/theme.js +13 -1
- package/dist/esm/limel-chart.entry.js +29 -18
- package/dist/esm/limel-markdown.entry.js +2 -2
- package/dist/esm/limel-prosemirror-adapter.entry.js +1 -1
- package/dist/esm/{markdown-exporter-B2vFxSLw.js → markdown-exporter-DcFcD699.js} +80 -897
- package/dist/esm/{markdown-parser-Cw8iW5R9.js → markdown-parser-8aHA9IYo.js} +991 -33
- package/dist/lime-elements/lime-elements.esm.js +1 -1
- package/dist/lime-elements/{p-8514480b.entry.js → p-0887e0df.entry.js} +2 -2
- package/dist/lime-elements/{p-1d9bc526.entry.js → p-9edd79a8.entry.js} +1 -1
- package/dist/lime-elements/p-C9gctvIB.js +1 -0
- package/dist/lime-elements/{p-c2f993cc.entry.js → p-a891f3e8.entry.js} +1 -1
- package/dist/lime-elements/p-kiQd8Ntf.js +7 -0
- package/dist/types/components/chart/theme.d.ts +2 -2
- package/package.json +3 -3
- package/dist/lime-elements/p-Bd37Vplf.js +0 -7
- package/dist/lime-elements/p-CvHpvp0f.js +0 -1
|
@@ -15068,7 +15068,7 @@ function markdownTable(table, options = {}) {
|
|
|
15068
15068
|
}
|
|
15069
15069
|
|
|
15070
15070
|
while (++columnIndex < table[rowIndex].length) {
|
|
15071
|
-
const cell = serialize(table[rowIndex][columnIndex]);
|
|
15071
|
+
const cell = serialize$1(table[rowIndex][columnIndex]);
|
|
15072
15072
|
|
|
15073
15073
|
if (options.alignDelimiters !== false) {
|
|
15074
15074
|
const size = stringLength(cell);
|
|
@@ -15239,7 +15239,7 @@ function markdownTable(table, options = {}) {
|
|
|
15239
15239
|
* @param {string|null|undefined} [value]
|
|
15240
15240
|
* @returns {string}
|
|
15241
15241
|
*/
|
|
15242
|
-
function serialize(value) {
|
|
15242
|
+
function serialize$1(value) {
|
|
15243
15243
|
return value === null || value === undefined ? '' : String(value)
|
|
15244
15244
|
}
|
|
15245
15245
|
|
|
@@ -15283,7 +15283,7 @@ function toAlignment(value) {
|
|
|
15283
15283
|
* Whether `value` is phrasing content.
|
|
15284
15284
|
*/
|
|
15285
15285
|
|
|
15286
|
-
const phrasing =
|
|
15286
|
+
const phrasing$1 =
|
|
15287
15287
|
/** @type {(node?: unknown) => node is PhrasingContent} */
|
|
15288
15288
|
(
|
|
15289
15289
|
index$1.convert([
|
|
@@ -15747,6 +15747,939 @@ function checkEmphasis(state) {
|
|
|
15747
15747
|
return marker
|
|
15748
15748
|
}
|
|
15749
15749
|
|
|
15750
|
+
/**
|
|
15751
|
+
* Get the first character of a value.
|
|
15752
|
+
*
|
|
15753
|
+
* Is aware of astral characters (such as emoji).
|
|
15754
|
+
*
|
|
15755
|
+
* @param {string} value
|
|
15756
|
+
* Value.
|
|
15757
|
+
* @returns {string}
|
|
15758
|
+
* First character; empty if `value` is empty.
|
|
15759
|
+
*/
|
|
15760
|
+
function firstCharacter(value) {
|
|
15761
|
+
const code = value.codePointAt(0);
|
|
15762
|
+
return code === undefined ? '' : String.fromCodePoint(code)
|
|
15763
|
+
}
|
|
15764
|
+
|
|
15765
|
+
/**
|
|
15766
|
+
* Get the last character of a value.
|
|
15767
|
+
*
|
|
15768
|
+
* Is aware of astral characters (such as emoji).
|
|
15769
|
+
*
|
|
15770
|
+
* @param {string} value
|
|
15771
|
+
* Value.
|
|
15772
|
+
* @returns {string}
|
|
15773
|
+
* Last character; empty if `value` is empty.
|
|
15774
|
+
*/
|
|
15775
|
+
function lastCharacter(value) {
|
|
15776
|
+
// Only a surrogate pair gives a code point above the BMP.
|
|
15777
|
+
const code = value.codePointAt(value.length - 2);
|
|
15778
|
+
return code !== undefined && code > 0xff_ff
|
|
15779
|
+
? value.slice(-2)
|
|
15780
|
+
: value.slice(-1)
|
|
15781
|
+
}
|
|
15782
|
+
|
|
15783
|
+
/**
|
|
15784
|
+
* Encode a code point as a character reference.
|
|
15785
|
+
*
|
|
15786
|
+
* @param {number} code
|
|
15787
|
+
* Code point to encode.
|
|
15788
|
+
* @returns {string}
|
|
15789
|
+
* Encoded character reference.
|
|
15790
|
+
*/
|
|
15791
|
+
function encodeCharacterReference(code) {
|
|
15792
|
+
return '&#x' + code.toString(16).toUpperCase() + ';'
|
|
15793
|
+
}
|
|
15794
|
+
|
|
15795
|
+
/**
|
|
15796
|
+
* @import {EncodeSides} from '../types.js'
|
|
15797
|
+
*/
|
|
15798
|
+
|
|
15799
|
+
|
|
15800
|
+
/**
|
|
15801
|
+
* Check whether to encode (as a character reference) the characters
|
|
15802
|
+
* surrounding an attention run.
|
|
15803
|
+
*
|
|
15804
|
+
* Which characters are around an attention run influence whether it works or
|
|
15805
|
+
* not.
|
|
15806
|
+
*
|
|
15807
|
+
* See <https://github.com/orgs/syntax-tree/discussions/60> for more info.
|
|
15808
|
+
* See this markdown in a particular renderer to see what works:
|
|
15809
|
+
*
|
|
15810
|
+
* ```markdown
|
|
15811
|
+
* | | A (letter inside) | B (punctuation inside) | C (whitespace inside) | D (nothing inside) |
|
|
15812
|
+
* | ----------------------- | ----------------- | ---------------------- | --------------------- | ------------------ |
|
|
15813
|
+
* | 1 (letter outside) | x*y*z | x*.*z | x* *z | x**z |
|
|
15814
|
+
* | 2 (punctuation outside) | .*y*. | .*.*. | .* *. | .**. |
|
|
15815
|
+
* | 3 (whitespace outside) | x *y* z | x *.* z | x * * z | x ** z |
|
|
15816
|
+
* | 4 (nothing outside) | *x* | *.* | * * | ** |
|
|
15817
|
+
* ```
|
|
15818
|
+
*
|
|
15819
|
+
* @param {number} outside
|
|
15820
|
+
* Code point on the outer side of the run.
|
|
15821
|
+
* @param {number} inside
|
|
15822
|
+
* Code point on the inner side of the run.
|
|
15823
|
+
* @param {string} marker
|
|
15824
|
+
* Marker of the run.
|
|
15825
|
+
* Underscores are handled more strictly (they form less often) than
|
|
15826
|
+
* asterisks.
|
|
15827
|
+
* @returns {EncodeSides}
|
|
15828
|
+
* Whether to encode characters.
|
|
15829
|
+
*/
|
|
15830
|
+
// Important: punctuation must never be encoded.
|
|
15831
|
+
// Punctuation is solely used by markdown constructs.
|
|
15832
|
+
// And by encoding itself.
|
|
15833
|
+
// Encoding them will break constructs or double encode things.
|
|
15834
|
+
function encodeInfo(outside, inside, marker) {
|
|
15835
|
+
const outsideKind = classifyCharacter(outside);
|
|
15836
|
+
const insideKind = classifyCharacter(inside);
|
|
15837
|
+
|
|
15838
|
+
// Letter outside:
|
|
15839
|
+
if (outsideKind === undefined) {
|
|
15840
|
+
return insideKind === undefined
|
|
15841
|
+
? // Letter inside:
|
|
15842
|
+
// we have to encode *both* letters for `_` as it is looser.
|
|
15843
|
+
// it already forms for `*` (and GFMs `~`).
|
|
15844
|
+
marker === '_'
|
|
15845
|
+
? {inside: true, outside: true}
|
|
15846
|
+
: {inside: false, outside: false}
|
|
15847
|
+
: insideKind === 1
|
|
15848
|
+
? // Whitespace inside: encode both (letter, whitespace).
|
|
15849
|
+
{inside: true, outside: true}
|
|
15850
|
+
: // Punctuation inside: encode outer (letter)
|
|
15851
|
+
{inside: false, outside: true}
|
|
15852
|
+
}
|
|
15853
|
+
|
|
15854
|
+
// Whitespace outside:
|
|
15855
|
+
if (outsideKind === 1) {
|
|
15856
|
+
return insideKind === undefined
|
|
15857
|
+
? // Letter inside: already forms.
|
|
15858
|
+
{inside: false, outside: false}
|
|
15859
|
+
: insideKind === 1
|
|
15860
|
+
? // Whitespace inside: encode both (whitespace).
|
|
15861
|
+
{inside: true, outside: true}
|
|
15862
|
+
: // Punctuation inside: already forms.
|
|
15863
|
+
{inside: false, outside: false}
|
|
15864
|
+
}
|
|
15865
|
+
|
|
15866
|
+
// Punctuation outside:
|
|
15867
|
+
return insideKind === undefined
|
|
15868
|
+
? // Letter inside: already forms.
|
|
15869
|
+
{inside: false, outside: false}
|
|
15870
|
+
: insideKind === 1
|
|
15871
|
+
? // Whitespace inside: encode inner (whitespace).
|
|
15872
|
+
{inside: true, outside: false}
|
|
15873
|
+
: // Punctuation inside: already forms.
|
|
15874
|
+
{inside: false, outside: false}
|
|
15875
|
+
}
|
|
15876
|
+
|
|
15877
|
+
/**
|
|
15878
|
+
* @typedef {1 | 2 | 3 | 4 | 5 | 6 | 7} Kind
|
|
15879
|
+
* Kind of HTML (flow) element.
|
|
15880
|
+
*/
|
|
15881
|
+
|
|
15882
|
+
/**
|
|
15883
|
+
* Infer the kind of HTML that `value` is.
|
|
15884
|
+
*
|
|
15885
|
+
* This works on valid trees as produced by `micromark`.
|
|
15886
|
+
* Kinds:
|
|
15887
|
+
*
|
|
15888
|
+
* 1. raw
|
|
15889
|
+
* 2. comment
|
|
15890
|
+
* 3. instruction
|
|
15891
|
+
* 4. declaration
|
|
15892
|
+
* 5. cdata
|
|
15893
|
+
* 6. block
|
|
15894
|
+
* 7. other
|
|
15895
|
+
*
|
|
15896
|
+
* See: <https://spec.commonmark.org/0.31.2/#html-blocks>.
|
|
15897
|
+
*
|
|
15898
|
+
* @param {string} value
|
|
15899
|
+
* HTML.
|
|
15900
|
+
* @returns {Kind | undefined}
|
|
15901
|
+
* Kind; `undefined` if unknown.
|
|
15902
|
+
*/
|
|
15903
|
+
function htmlKind(value) {
|
|
15904
|
+
if (value.charCodeAt(0) !== 60 /* `<` */) return
|
|
15905
|
+
|
|
15906
|
+
const next = value.charCodeAt(1);
|
|
15907
|
+
|
|
15908
|
+
if (next === 33 /* `!` */) {
|
|
15909
|
+
const code = value.charCodeAt(2);
|
|
15910
|
+
if (code === 45 /* `-` */) return 2
|
|
15911
|
+
if (code === 91 /* `[` */) return 5
|
|
15912
|
+
if (asciiAlpha(code)) return 4
|
|
15913
|
+
return
|
|
15914
|
+
}
|
|
15915
|
+
|
|
15916
|
+
if (next === 63 /* `?` */) return 3
|
|
15917
|
+
|
|
15918
|
+
const closing = next === 47; /* `/` */
|
|
15919
|
+
const start = closing ? 2 : 1;
|
|
15920
|
+
let end = start;
|
|
15921
|
+
let code = value.charCodeAt(end);
|
|
15922
|
+
|
|
15923
|
+
if (!asciiAlpha(code)) return
|
|
15924
|
+
|
|
15925
|
+
while (code === 45 /* `-` */ || asciiAlphanumeric(code)) {
|
|
15926
|
+
code = value.charCodeAt(++end);
|
|
15927
|
+
}
|
|
15928
|
+
|
|
15929
|
+
const name = value.slice(start, end).toLowerCase();
|
|
15930
|
+
|
|
15931
|
+
if (!closing && code !== 47 /* `/` */ && htmlRawNames.includes(name)) {
|
|
15932
|
+
return 1
|
|
15933
|
+
}
|
|
15934
|
+
|
|
15935
|
+
return htmlBlockNames.includes(name) ? 6 : 7
|
|
15936
|
+
}
|
|
15937
|
+
|
|
15938
|
+
/**
|
|
15939
|
+
* @import {Attention, Handle, Info, State, Tracker} from 'mdast-util-to-markdown'
|
|
15940
|
+
* @import {PhrasingParents} from '../types.js'
|
|
15941
|
+
*/
|
|
15942
|
+
|
|
15943
|
+
|
|
15944
|
+
/**
|
|
15945
|
+
* Markers for builtin attention (emphasis, strong) to whether they are stricter:
|
|
15946
|
+
* whether they cannot open and close inside words.
|
|
15947
|
+
*
|
|
15948
|
+
* Builtin runs pair following CommonMark rules for emphasis and strong:
|
|
15949
|
+
* they are split,
|
|
15950
|
+
* using one or two markers at a time,
|
|
15951
|
+
* with the rule of 3.
|
|
15952
|
+
* Other markers pair like GFM strikethrough:
|
|
15953
|
+
* whole runs pair with a run of the same size.
|
|
15954
|
+
*
|
|
15955
|
+
* @type {Map<string, boolean>}
|
|
15956
|
+
*/
|
|
15957
|
+
const builtins = new Map([
|
|
15958
|
+
['*', false],
|
|
15959
|
+
['_', true]
|
|
15960
|
+
]);
|
|
15961
|
+
|
|
15962
|
+
/**
|
|
15963
|
+
* @typedef AttentionResult
|
|
15964
|
+
* Attention, of which the sequence is chosen later.
|
|
15965
|
+
* @property {Array<Item>} children
|
|
15966
|
+
* Serialized children.
|
|
15967
|
+
* @property {Array<string>} sequences
|
|
15968
|
+
* Sequences that can be used, in order of preference.
|
|
15969
|
+
*/
|
|
15970
|
+
|
|
15971
|
+
/**
|
|
15972
|
+
* @typedef Improvement
|
|
15973
|
+
* Improvement of attention sequences.
|
|
15974
|
+
* @property {Map<AttentionResult, string>} chosen
|
|
15975
|
+
* Chosen sequences for each attention.
|
|
15976
|
+
* @property {Array<Token>} tokens
|
|
15977
|
+
* Tokens involved in the improvement.
|
|
15978
|
+
* @property {Mistake | undefined} mistake
|
|
15979
|
+
* Mistake that led to this improvement, if any.
|
|
15980
|
+
*/
|
|
15981
|
+
|
|
15982
|
+
/**
|
|
15983
|
+
* @typedef {AttentionResult | string} Item
|
|
15984
|
+
* Serialized phrasing.
|
|
15985
|
+
*/
|
|
15986
|
+
|
|
15987
|
+
/**
|
|
15988
|
+
* @typedef Mistake
|
|
15989
|
+
* Sequence that does not form its attention.
|
|
15990
|
+
* @property {Array<AttentionResult>} attention
|
|
15991
|
+
* Attention to try other sequences for.
|
|
15992
|
+
* @property {number} index
|
|
15993
|
+
* Index of the token.
|
|
15994
|
+
*/
|
|
15995
|
+
|
|
15996
|
+
/**
|
|
15997
|
+
* @typedef Run
|
|
15998
|
+
* Sequences next to each other with the same marker.
|
|
15999
|
+
* @property {boolean} close
|
|
16000
|
+
* Whether it can close.
|
|
16001
|
+
* @property {number} end
|
|
16002
|
+
* Index after the last marker not used (to open).
|
|
16003
|
+
* @property {Array<Token>} markers
|
|
16004
|
+
* Sequence of each marker.
|
|
16005
|
+
* @property {boolean} open
|
|
16006
|
+
* Whether it can open.
|
|
16007
|
+
* @property {boolean} split
|
|
16008
|
+
* Whether it can be split (built-in attention).
|
|
16009
|
+
* @property {number} start
|
|
16010
|
+
* Index of the first marker not used (to close).
|
|
16011
|
+
* @property {Array<Token>} tokens
|
|
16012
|
+
* Sequences.
|
|
16013
|
+
*/
|
|
16014
|
+
|
|
16015
|
+
/**
|
|
16016
|
+
* @typedef Token
|
|
16017
|
+
* Rendered phrasing.
|
|
16018
|
+
* @property {AttentionResult | undefined} attention
|
|
16019
|
+
* Attention, if this is one of its sequences.
|
|
16020
|
+
* @property {string} value
|
|
16021
|
+
* Value.
|
|
16022
|
+
*/
|
|
16023
|
+
|
|
16024
|
+
/**
|
|
16025
|
+
* Get candidates for a mistake:
|
|
16026
|
+
* attention to try other sequences for,
|
|
16027
|
+
* later first.
|
|
16028
|
+
*
|
|
16029
|
+
* @param {Array<Token>} tokens
|
|
16030
|
+
* Tokens.
|
|
16031
|
+
* @param {Array<Token>} involved
|
|
16032
|
+
* Sequences involved in the mistake.
|
|
16033
|
+
* @param {Map<Token, Run>} runOf
|
|
16034
|
+
* Run of each sequence.
|
|
16035
|
+
* @returns {Array<AttentionResult>}
|
|
16036
|
+
* Candidates.
|
|
16037
|
+
*/
|
|
16038
|
+
function candidates(tokens, involved, runOf) {
|
|
16039
|
+
/** @type {Set<AttentionResult | undefined>} */
|
|
16040
|
+
const related = new Set();
|
|
16041
|
+
|
|
16042
|
+
// The attention involved, and attention in runs with its sequences.
|
|
16043
|
+
for (const token of tokens) {
|
|
16044
|
+
const siblings = runOf.get(token);
|
|
16045
|
+
if (
|
|
16046
|
+
siblings &&
|
|
16047
|
+
token.attention &&
|
|
16048
|
+
involved.some((d) => d.attention === token.attention)
|
|
16049
|
+
) {
|
|
16050
|
+
for (const sibling of siblings.tokens) {
|
|
16051
|
+
related.add(sibling.attention);
|
|
16052
|
+
}
|
|
16053
|
+
}
|
|
16054
|
+
}
|
|
16055
|
+
|
|
16056
|
+
/** @type {Array<AttentionResult>} */
|
|
16057
|
+
const ordered = [];
|
|
16058
|
+
|
|
16059
|
+
for (const token of tokens) {
|
|
16060
|
+
if (token.attention && related.delete(token.attention)) {
|
|
16061
|
+
ordered.push(token.attention);
|
|
16062
|
+
}
|
|
16063
|
+
}
|
|
16064
|
+
|
|
16065
|
+
// Later first: attention that opens later is inner or next.
|
|
16066
|
+
ordered.reverse();
|
|
16067
|
+
return ordered
|
|
16068
|
+
}
|
|
16069
|
+
|
|
16070
|
+
/**
|
|
16071
|
+
* Check whether sequences form their attention, like `micromark`.
|
|
16072
|
+
*
|
|
16073
|
+
* Sequences of `*` and `_` pair like emphasis and strong in CommonMark,
|
|
16074
|
+
* others like strikethrough in GFM.
|
|
16075
|
+
*
|
|
16076
|
+
* @param {Array<Token>} tokens
|
|
16077
|
+
* Tokens.
|
|
16078
|
+
* @param {string} before
|
|
16079
|
+
* Characters before.
|
|
16080
|
+
* @param {string} after
|
|
16081
|
+
* Characters after.
|
|
16082
|
+
* @returns {Mistake | undefined}
|
|
16083
|
+
* First mistake, if any.
|
|
16084
|
+
*/
|
|
16085
|
+
function check(tokens, before, after) {
|
|
16086
|
+
/** @type {Array<Run>} */
|
|
16087
|
+
const runs = [];
|
|
16088
|
+
/** @type {Map<Token, Array<Token>>} */
|
|
16089
|
+
const wrong = new Map();
|
|
16090
|
+
/** @type {Map<Token, Run>} */
|
|
16091
|
+
const runOf = new Map();
|
|
16092
|
+
let index = 0;
|
|
16093
|
+
|
|
16094
|
+
while (index < tokens.length) {
|
|
16095
|
+
if (!tokens[index].attention) {
|
|
16096
|
+
index++;
|
|
16097
|
+
continue
|
|
16098
|
+
}
|
|
16099
|
+
|
|
16100
|
+
const marker = tokens[index].value.charAt(0);
|
|
16101
|
+
const strict = builtins.get(marker);
|
|
16102
|
+
let end = index + 1;
|
|
16103
|
+
|
|
16104
|
+
while (
|
|
16105
|
+
end < tokens.length &&
|
|
16106
|
+
tokens[end].attention &&
|
|
16107
|
+
tokens[end].value.charAt(0) === marker
|
|
16108
|
+
) {
|
|
16109
|
+
end++;
|
|
16110
|
+
}
|
|
16111
|
+
|
|
16112
|
+
const head = index ? tokens[index - 1].value : before;
|
|
16113
|
+
const previous = classifyCharacter(head.charCodeAt(head.length - 1));
|
|
16114
|
+
const next = classifyCharacter(
|
|
16115
|
+
(end < tokens.length ? tokens[end].value : after).charCodeAt(0)
|
|
16116
|
+
);
|
|
16117
|
+
const open = !next || (next === 2 && Boolean(previous));
|
|
16118
|
+
const close = !previous || (previous === 2 && Boolean(next));
|
|
16119
|
+
/** @type {Run} */
|
|
16120
|
+
const run = {
|
|
16121
|
+
tokens: tokens.slice(index, end),
|
|
16122
|
+
markers: [],
|
|
16123
|
+
start: 0,
|
|
16124
|
+
end: 0,
|
|
16125
|
+
// Underscores are strict, they cannot open or close inside words.
|
|
16126
|
+
open: strict ? open && (Boolean(previous) || !close) : open,
|
|
16127
|
+
close: strict ? close && (Boolean(next) || !open) : close,
|
|
16128
|
+
// All builtins can split runs.
|
|
16129
|
+
split: strict !== undefined
|
|
16130
|
+
};
|
|
16131
|
+
|
|
16132
|
+
for (const token of run.tokens) {
|
|
16133
|
+
// Every sequence is wrong until it pairs with its other sequence.
|
|
16134
|
+
wrong.set(token, [...run.tokens]);
|
|
16135
|
+
runOf.set(token, run);
|
|
16136
|
+
let size = token.value.length;
|
|
16137
|
+
while (size--) run.markers.push(token);
|
|
16138
|
+
}
|
|
16139
|
+
|
|
16140
|
+
run.end = run.markers.length;
|
|
16141
|
+
runs.push(run);
|
|
16142
|
+
index = end;
|
|
16143
|
+
}
|
|
16144
|
+
|
|
16145
|
+
pair(runs, wrong);
|
|
16146
|
+
|
|
16147
|
+
index = -1;
|
|
16148
|
+
|
|
16149
|
+
while (++index < tokens.length) {
|
|
16150
|
+
const involved = wrong.get(tokens[index]);
|
|
16151
|
+
if (involved) return {attention: candidates(tokens, involved, runOf), index}
|
|
16152
|
+
}
|
|
16153
|
+
}
|
|
16154
|
+
|
|
16155
|
+
/**
|
|
16156
|
+
* Serialize the children of a parent that contains phrasing children.
|
|
16157
|
+
*
|
|
16158
|
+
* @param {PhrasingParents} parent
|
|
16159
|
+
* Parent of phrasing nodes.
|
|
16160
|
+
* @param {State} state
|
|
16161
|
+
* Info passed around about the current state.
|
|
16162
|
+
* @param {Info} info
|
|
16163
|
+
* Info on where we are in the document we are generating.
|
|
16164
|
+
* @returns {string}
|
|
16165
|
+
* Serialized result.
|
|
16166
|
+
*/
|
|
16167
|
+
function containerPhrasing(parent, state, info) {
|
|
16168
|
+
return serialize(phrasing(parent, state, info), info.before, info.after)
|
|
16169
|
+
}
|
|
16170
|
+
|
|
16171
|
+
/**
|
|
16172
|
+
* Encode the first or last character of tokens as a character reference,
|
|
16173
|
+
* if it is text.
|
|
16174
|
+
*
|
|
16175
|
+
* @param {Array<Token>} tokens
|
|
16176
|
+
* Tokens.
|
|
16177
|
+
* @param {boolean} start
|
|
16178
|
+
* Whether to encode the first or last character.
|
|
16179
|
+
* @returns {undefined}
|
|
16180
|
+
* Nothing.
|
|
16181
|
+
*/
|
|
16182
|
+
function encode(tokens, start) {
|
|
16183
|
+
const token = tokens[start ? 0 : tokens.length - 1];
|
|
16184
|
+
|
|
16185
|
+
if (!token || token.attention) return
|
|
16186
|
+
|
|
16187
|
+
const character = start
|
|
16188
|
+
? firstCharacter(token.value)
|
|
16189
|
+
: lastCharacter(token.value);
|
|
16190
|
+
const reference = encodeCharacterReference(
|
|
16191
|
+
/** @type {number} */ (character.codePointAt(0))
|
|
16192
|
+
);
|
|
16193
|
+
|
|
16194
|
+
if (start) {
|
|
16195
|
+
token.value = reference + token.value.slice(character.length);
|
|
16196
|
+
} else {
|
|
16197
|
+
let value = token.value.slice(0, token.value.length - character.length);
|
|
16198
|
+
let escapes = 0;
|
|
16199
|
+
|
|
16200
|
+
while (value.charCodeAt(value.length - 1 - escapes) === 92 /* `\` */) {
|
|
16201
|
+
escapes++;
|
|
16202
|
+
}
|
|
16203
|
+
|
|
16204
|
+
// Odd number would escape the `&` again.
|
|
16205
|
+
if (escapes % 2) value += '\\';
|
|
16206
|
+
token.value = value + reference;
|
|
16207
|
+
}
|
|
16208
|
+
}
|
|
16209
|
+
|
|
16210
|
+
/**
|
|
16211
|
+
* Try other sequences for the candidates of a mistake and use the first that
|
|
16212
|
+
* moves the mistake further.
|
|
16213
|
+
*
|
|
16214
|
+
* @param {Array<Item>} items
|
|
16215
|
+
* Items.
|
|
16216
|
+
* @param {Map<AttentionResult, string>} chosen
|
|
16217
|
+
* Sequences to use instead of preferred sequences.
|
|
16218
|
+
* @param {Mistake} mistake
|
|
16219
|
+
* Mistake.
|
|
16220
|
+
* @param {string} before
|
|
16221
|
+
* Characters before.
|
|
16222
|
+
* @param {string} after
|
|
16223
|
+
* Characters after.
|
|
16224
|
+
* @returns {Improvement | undefined}
|
|
16225
|
+
* Result if better.
|
|
16226
|
+
*/
|
|
16227
|
+
function improve(items, chosen, mistake, before, after) {
|
|
16228
|
+
for (const attention of mistake.attention) {
|
|
16229
|
+
const current = chosen.get(attention) || attention.sequences[0];
|
|
16230
|
+
|
|
16231
|
+
for (const sequence of attention.sequences) {
|
|
16232
|
+
if (sequence === current) continue
|
|
16233
|
+
const trial = new Map(chosen);
|
|
16234
|
+
trial.set(attention, sequence);
|
|
16235
|
+
const tokens = render(items, trial, before, after);
|
|
16236
|
+
const next = check(tokens, before, after);
|
|
16237
|
+
|
|
16238
|
+
if (!next || next.index > mistake.index) {
|
|
16239
|
+
return {chosen: trial, mistake: next, tokens}
|
|
16240
|
+
}
|
|
16241
|
+
}
|
|
16242
|
+
}
|
|
16243
|
+
}
|
|
16244
|
+
|
|
16245
|
+
/**
|
|
16246
|
+
* Pair runs like `micromark` and mark sequences that do form their attention
|
|
16247
|
+
* as fine.
|
|
16248
|
+
*
|
|
16249
|
+
* Each pair uses markers from the end of an opener and the start of a closer.
|
|
16250
|
+
* It forms the intended attention if those markers are exactly the whole
|
|
16251
|
+
* opening and closing sequence of one attention.
|
|
16252
|
+
* Otherwise attention is mixed up:
|
|
16253
|
+
* either sequences of different attention pair,
|
|
16254
|
+
* part of a sequence is used (such as `**` of strong as `*`),
|
|
16255
|
+
* or several sequences are used together (such as `*` and `*` as `**`).
|
|
16256
|
+
* Then the sequences involved are recorded with each other.
|
|
16257
|
+
*
|
|
16258
|
+
* @param {Array<Run>} runs
|
|
16259
|
+
* Runs.
|
|
16260
|
+
* @param {Map<Token, Array<Token>>} wrong
|
|
16261
|
+
* Sequences that are wrong, with the sequences involved.
|
|
16262
|
+
* @returns {undefined}
|
|
16263
|
+
* Nothing, `wrong` is changed.
|
|
16264
|
+
*/
|
|
16265
|
+
function pair(runs, wrong) {
|
|
16266
|
+
/** @type {Map<string, Array<Run>>} */
|
|
16267
|
+
const openersByMarker = new Map();
|
|
16268
|
+
|
|
16269
|
+
for (const closer of runs) {
|
|
16270
|
+
const marker = closer.tokens[0].value.charAt(0);
|
|
16271
|
+
let openers = openersByMarker.get(marker);
|
|
16272
|
+
|
|
16273
|
+
if (!openers) {
|
|
16274
|
+
openers = [];
|
|
16275
|
+
openersByMarker.set(marker, openers);
|
|
16276
|
+
}
|
|
16277
|
+
|
|
16278
|
+
let open = closer.close ? openers.length : 0;
|
|
16279
|
+
|
|
16280
|
+
while (open-- && closer.start < closer.end) {
|
|
16281
|
+
const opener = openers[open];
|
|
16282
|
+
const openerSize = opener.end - opener.start;
|
|
16283
|
+
const closerSize = closer.end - closer.start;
|
|
16284
|
+
|
|
16285
|
+
if (
|
|
16286
|
+
closer.split
|
|
16287
|
+
? // The rule of 3, on the sizes that are left.
|
|
16288
|
+
(opener.close || closer.open) &&
|
|
16289
|
+
closerSize % 3 &&
|
|
16290
|
+
!((openerSize + closerSize) % 3)
|
|
16291
|
+
: // Others pair whole runs of the same size.
|
|
16292
|
+
openerSize !== opener.markers.length || openerSize !== closerSize
|
|
16293
|
+
) {
|
|
16294
|
+
continue
|
|
16295
|
+
}
|
|
16296
|
+
|
|
16297
|
+
const size = closer.split
|
|
16298
|
+
? openerSize > 1 && closerSize > 1
|
|
16299
|
+
? 2
|
|
16300
|
+
: 1
|
|
16301
|
+
: closerSize;
|
|
16302
|
+
const opening = opener.markers.slice(opener.end - size, opener.end);
|
|
16303
|
+
const closing = closer.markers.slice(closer.start, closer.start + size);
|
|
16304
|
+
const openToken = opening[0];
|
|
16305
|
+
const closeToken = closing[0];
|
|
16306
|
+
|
|
16307
|
+
if (
|
|
16308
|
+
// Whole sequences:
|
|
16309
|
+
// the markers of a sequence are next to each other,
|
|
16310
|
+
// so if the first and last marker are of one sequence of that size,
|
|
16311
|
+
// all markers are exactly that sequence.
|
|
16312
|
+
openToken === opening[size - 1] &&
|
|
16313
|
+
openToken.value.length === size &&
|
|
16314
|
+
closeToken === closing[size - 1] &&
|
|
16315
|
+
closeToken.value.length === size &&
|
|
16316
|
+
// Of the same attention, so its opening and closing sequence.
|
|
16317
|
+
openToken.attention === closeToken.attention
|
|
16318
|
+
) {
|
|
16319
|
+
wrong.delete(openToken);
|
|
16320
|
+
wrong.delete(closeToken);
|
|
16321
|
+
} else {
|
|
16322
|
+
const involved = [...new Set([...opening, ...closing])];
|
|
16323
|
+
// Add to what is involved already: a sequence can be in several pairs.
|
|
16324
|
+
for (const token of involved) {
|
|
16325
|
+
const list = wrong.get(token);
|
|
16326
|
+
if (list) list.push(...involved);
|
|
16327
|
+
}
|
|
16328
|
+
}
|
|
16329
|
+
|
|
16330
|
+
opener.end -= size;
|
|
16331
|
+
closer.start += size;
|
|
16332
|
+
|
|
16333
|
+
if (opener.start === opener.end) openers.splice(open, 1);
|
|
16334
|
+
|
|
16335
|
+
// Search again from the start, as this run may close more.
|
|
16336
|
+
open = openers.length;
|
|
16337
|
+
}
|
|
16338
|
+
|
|
16339
|
+
if (closer.open && closer.start < closer.end) openers.push(closer);
|
|
16340
|
+
}
|
|
16341
|
+
}
|
|
16342
|
+
|
|
16343
|
+
/**
|
|
16344
|
+
* Get the sequences that can be used for attention, in order of preference.
|
|
16345
|
+
*
|
|
16346
|
+
* @param {string} type
|
|
16347
|
+
* Node type.
|
|
16348
|
+
* @param {Array<string>} markers
|
|
16349
|
+
* Markers.
|
|
16350
|
+
* @param {Array<number>} sizes
|
|
16351
|
+
* Sizes.
|
|
16352
|
+
* @returns {Array<string>}
|
|
16353
|
+
* Sequences.
|
|
16354
|
+
*/
|
|
16355
|
+
function attentionSequences(type, markers, sizes) {
|
|
16356
|
+
if (markers.length === 0) {
|
|
16357
|
+
throw new Error(
|
|
16358
|
+
'Cannot serialize `' +
|
|
16359
|
+
type +
|
|
16360
|
+
'` as attention without markers, expected one or more markers'
|
|
16361
|
+
)
|
|
16362
|
+
}
|
|
16363
|
+
|
|
16364
|
+
if (sizes.length === 0) {
|
|
16365
|
+
throw new Error(
|
|
16366
|
+
'Cannot serialize `' +
|
|
16367
|
+
type +
|
|
16368
|
+
'` as attention without sizes, expected one or more sizes'
|
|
16369
|
+
)
|
|
16370
|
+
}
|
|
16371
|
+
|
|
16372
|
+
for (const size of sizes) {
|
|
16373
|
+
if (!Number.isSafeInteger(size) || size < 1) {
|
|
16374
|
+
throw new Error(
|
|
16375
|
+
'Cannot serialize `' +
|
|
16376
|
+
type +
|
|
16377
|
+
'` as attention with `' +
|
|
16378
|
+
size +
|
|
16379
|
+
'` as size, expected positive integer'
|
|
16380
|
+
)
|
|
16381
|
+
}
|
|
16382
|
+
}
|
|
16383
|
+
|
|
16384
|
+
/** @type {Array<string>} */
|
|
16385
|
+
const sequences = [];
|
|
16386
|
+
|
|
16387
|
+
for (const marker of markers) {
|
|
16388
|
+
if (marker.length !== 1) {
|
|
16389
|
+
throw new Error(
|
|
16390
|
+
'Cannot serialize `' +
|
|
16391
|
+
type +
|
|
16392
|
+
'` as attention with `' +
|
|
16393
|
+
marker +
|
|
16394
|
+
'` as marker, expected a single ascii character'
|
|
16395
|
+
)
|
|
16396
|
+
}
|
|
16397
|
+
|
|
16398
|
+
if (!asciiPunctuation(marker.charCodeAt(0))) {
|
|
16399
|
+
throw new Error(
|
|
16400
|
+
'Cannot serialize `' +
|
|
16401
|
+
type +
|
|
16402
|
+
'` as attention with `' +
|
|
16403
|
+
marker +
|
|
16404
|
+
'` as marker, expected ascii punctuation'
|
|
16405
|
+
)
|
|
16406
|
+
}
|
|
16407
|
+
|
|
16408
|
+
for (const size of sizes) {
|
|
16409
|
+
sequences.push(marker.repeat(size));
|
|
16410
|
+
}
|
|
16411
|
+
}
|
|
16412
|
+
|
|
16413
|
+
return sequences
|
|
16414
|
+
}
|
|
16415
|
+
|
|
16416
|
+
/**
|
|
16417
|
+
* Serialize the children of a parent that contains phrasing children,
|
|
16418
|
+
* keeping attention separate,
|
|
16419
|
+
* so that its markers can be chosen later.
|
|
16420
|
+
*
|
|
16421
|
+
* @param {PhrasingParents} parent
|
|
16422
|
+
* Parent of phrasing nodes.
|
|
16423
|
+
* @param {State} state
|
|
16424
|
+
* Info passed around about the current state.
|
|
16425
|
+
* @param {Info} info
|
|
16426
|
+
* Info on where we are in the document we are generating.
|
|
16427
|
+
* @returns {Array<Item>}
|
|
16428
|
+
* Items.
|
|
16429
|
+
*/
|
|
16430
|
+
function phrasing(parent, state, info) {
|
|
16431
|
+
const indexStack = state.indexStack;
|
|
16432
|
+
const children = parent.children || [];
|
|
16433
|
+
/** @type {Array<Item>} */
|
|
16434
|
+
const results = [];
|
|
16435
|
+
let index = -1;
|
|
16436
|
+
let before = info.before;
|
|
16437
|
+
|
|
16438
|
+
indexStack.push(-1);
|
|
16439
|
+
let tracker = state.createTracker(info);
|
|
16440
|
+
|
|
16441
|
+
while (++index < children.length) {
|
|
16442
|
+
const child = children[index];
|
|
16443
|
+
/** @type {string} */
|
|
16444
|
+
let after;
|
|
16445
|
+
|
|
16446
|
+
indexStack[indexStack.length - 1] = index;
|
|
16447
|
+
|
|
16448
|
+
if (index + 1 < children.length) {
|
|
16449
|
+
/** @type {Handle} */
|
|
16450
|
+
// @ts-expect-error: hush, it’s actually a `zwitch`.
|
|
16451
|
+
let handle = state.handle.handlers[children[index + 1].type];
|
|
16452
|
+
// @ts-expect-error: hush, it’s actually a `zwitch`.
|
|
16453
|
+
if (handle && handle.peek) handle = handle.peek;
|
|
16454
|
+
after = handle
|
|
16455
|
+
? firstCharacter(
|
|
16456
|
+
handle(children[index + 1], parent, state, {
|
|
16457
|
+
before: '',
|
|
16458
|
+
after: '',
|
|
16459
|
+
...tracker.current()
|
|
16460
|
+
})
|
|
16461
|
+
)
|
|
16462
|
+
: '';
|
|
16463
|
+
} else {
|
|
16464
|
+
after = info.after;
|
|
16465
|
+
}
|
|
16466
|
+
|
|
16467
|
+
// In some cases, html (text) can be found in phrasing right after an eol.
|
|
16468
|
+
// When we’d serialize that, it could be seen as html (flow): all its kinds
|
|
16469
|
+
// except 7 can interrupt a paragraph.
|
|
16470
|
+
// As we can’t escape or so to prevent it from happening,
|
|
16471
|
+
// we take a somewhat reasonable approach:
|
|
16472
|
+
// replace that eol with a space.
|
|
16473
|
+
// See: <https://github.com/syntax-tree/mdast-util-to-markdown/issues/15>
|
|
16474
|
+
const previous = results[results.length - 1];
|
|
16475
|
+
|
|
16476
|
+
if (
|
|
16477
|
+
typeof previous === 'string' &&
|
|
16478
|
+
(before === '\r' || before === '\n') &&
|
|
16479
|
+
child.type === 'html' &&
|
|
16480
|
+
htmlKind(child.value) !== 7
|
|
16481
|
+
) {
|
|
16482
|
+
results[results.length - 1] = previous.replace(/(\r?\n|\r)$/, ' ');
|
|
16483
|
+
before = ' ';
|
|
16484
|
+
|
|
16485
|
+
// To do: does this work to reset tracker?
|
|
16486
|
+
tracker = state.createTracker(info);
|
|
16487
|
+
tracker.move(serialize(results, info.before, info.after));
|
|
16488
|
+
}
|
|
16489
|
+
|
|
16490
|
+
/** @type {(Handle & {attention?: Attention | undefined}) | undefined} */
|
|
16491
|
+
// @ts-expect-error: hush, it’s actually a `zwitch`.
|
|
16492
|
+
const handle = state.handle.handlers[child.type];
|
|
16493
|
+
|
|
16494
|
+
if (handle && handle.attention) {
|
|
16495
|
+
const item = attentionItem(
|
|
16496
|
+
/** @type {PhrasingParents} */ (child),
|
|
16497
|
+
handle.attention,
|
|
16498
|
+
state,
|
|
16499
|
+
tracker
|
|
16500
|
+
);
|
|
16501
|
+
results.push(item);
|
|
16502
|
+
before = lastCharacter(item.sequences[0]);
|
|
16503
|
+
} else {
|
|
16504
|
+
const value = state.handle(child, parent, state, {
|
|
16505
|
+
...tracker.current(),
|
|
16506
|
+
after,
|
|
16507
|
+
before
|
|
16508
|
+
});
|
|
16509
|
+
|
|
16510
|
+
if (!value) continue
|
|
16511
|
+
tracker.move(value);
|
|
16512
|
+
results.push(value);
|
|
16513
|
+
before = lastCharacter(value);
|
|
16514
|
+
}
|
|
16515
|
+
}
|
|
16516
|
+
|
|
16517
|
+
indexStack.pop();
|
|
16518
|
+
|
|
16519
|
+
return results
|
|
16520
|
+
}
|
|
16521
|
+
|
|
16522
|
+
/**
|
|
16523
|
+
* Serialize a node as attention,
|
|
16524
|
+
* with its sequence chosen later.
|
|
16525
|
+
*
|
|
16526
|
+
* @param {PhrasingParents} node
|
|
16527
|
+
* Node.
|
|
16528
|
+
* @param {Attention} attention
|
|
16529
|
+
* Attention of the node.
|
|
16530
|
+
* @param {State} state
|
|
16531
|
+
* Info passed around about the current state.
|
|
16532
|
+
* @param {Tracker} tracker
|
|
16533
|
+
* Tracker.
|
|
16534
|
+
* @returns {AttentionResult}
|
|
16535
|
+
* Attention.
|
|
16536
|
+
*/
|
|
16537
|
+
function attentionItem(node, attention, state, tracker) {
|
|
16538
|
+
const {construct, markers, sizes} = attention(node, state);
|
|
16539
|
+
const sequences = attentionSequences(node.type, markers, sizes);
|
|
16540
|
+
const sequence = sequences[0];
|
|
16541
|
+
const exit = state.enter(construct);
|
|
16542
|
+
tracker.move(sequence);
|
|
16543
|
+
const children = phrasing(node, state, {
|
|
16544
|
+
...tracker.current(),
|
|
16545
|
+
before: sequence,
|
|
16546
|
+
after: sequence
|
|
16547
|
+
});
|
|
16548
|
+
tracker.move(serialize(children, sequence, sequence));
|
|
16549
|
+
tracker.move(sequence);
|
|
16550
|
+
exit();
|
|
16551
|
+
return {children, sequences}
|
|
16552
|
+
}
|
|
16553
|
+
|
|
16554
|
+
/**
|
|
16555
|
+
* Render items with sequences;
|
|
16556
|
+
* encoding characters around attention where needed.
|
|
16557
|
+
*
|
|
16558
|
+
* @param {Array<Item>} items
|
|
16559
|
+
* Serialized phrasing.
|
|
16560
|
+
* @param {Map<AttentionResult, string>} chosen
|
|
16561
|
+
* Sequences to use instead of preferred sequences.
|
|
16562
|
+
* @param {string} before
|
|
16563
|
+
* Characters before.
|
|
16564
|
+
* @param {string} after
|
|
16565
|
+
* Characters after.
|
|
16566
|
+
* @returns {Array<Token>}
|
|
16567
|
+
* Tokens.
|
|
16568
|
+
*/
|
|
16569
|
+
function render(items, chosen, before, after) {
|
|
16570
|
+
/** @type {Array<Token>} */
|
|
16571
|
+
const tokens = [];
|
|
16572
|
+
/** @type {string | undefined} */
|
|
16573
|
+
let encodeAfter;
|
|
16574
|
+
let previous = lastCharacter(before);
|
|
16575
|
+
let index = -1;
|
|
16576
|
+
|
|
16577
|
+
while (++index < items.length) {
|
|
16578
|
+
const item = items[index];
|
|
16579
|
+
|
|
16580
|
+
if (typeof item === 'string') {
|
|
16581
|
+
/** @type {Token} */
|
|
16582
|
+
const token = {value: item, attention: undefined};
|
|
16583
|
+
|
|
16584
|
+
// If attention before had to encode the character after it, encode it.
|
|
16585
|
+
if (encodeAfter && encodeAfter === firstCharacter(item)) {
|
|
16586
|
+
encode([token], true);
|
|
16587
|
+
}
|
|
16588
|
+
|
|
16589
|
+
encodeAfter = undefined;
|
|
16590
|
+
tokens.push(token);
|
|
16591
|
+
if (token.value) previous = lastCharacter(token.value);
|
|
16592
|
+
continue
|
|
16593
|
+
}
|
|
16594
|
+
|
|
16595
|
+
const sequence = chosen.get(item) || item.sequences[0];
|
|
16596
|
+
const marker = sequence.charAt(0);
|
|
16597
|
+
const next = items[index + 1];
|
|
16598
|
+
const outsideBefore = previous;
|
|
16599
|
+
const outsideAfter = firstCharacter(
|
|
16600
|
+
next === undefined
|
|
16601
|
+
? after
|
|
16602
|
+
: typeof next === 'string'
|
|
16603
|
+
? next
|
|
16604
|
+
: chosen.get(next) || next.sequences[0]
|
|
16605
|
+
);
|
|
16606
|
+
const inside = render(item.children, chosen, sequence, sequence);
|
|
16607
|
+
const head = inside.length > 0 ? inside[0].value : '';
|
|
16608
|
+
const open = encodeInfo(
|
|
16609
|
+
outsideBefore.charCodeAt(outsideBefore.length - 1),
|
|
16610
|
+
head.charCodeAt(0),
|
|
16611
|
+
marker
|
|
16612
|
+
);
|
|
16613
|
+
|
|
16614
|
+
if (open.inside) encode(inside, true);
|
|
16615
|
+
|
|
16616
|
+
const tail =
|
|
16617
|
+
inside.length > 0 ? lastCharacter(inside[inside.length - 1].value) : '';
|
|
16618
|
+
const close = encodeInfo(
|
|
16619
|
+
outsideAfter.charCodeAt(0),
|
|
16620
|
+
tail.charCodeAt(tail.length - 1),
|
|
16621
|
+
marker
|
|
16622
|
+
);
|
|
16623
|
+
|
|
16624
|
+
if (close.inside) encode(inside, false);
|
|
16625
|
+
|
|
16626
|
+
if (open.outside && outsideBefore !== '\n' && outsideBefore !== '\r') {
|
|
16627
|
+
encode(tokens, false);
|
|
16628
|
+
}
|
|
16629
|
+
|
|
16630
|
+
if (close.outside) encodeAfter = outsideAfter;
|
|
16631
|
+
|
|
16632
|
+
tokens.push({value: sequence, attention: item}, ...inside, {
|
|
16633
|
+
value: sequence,
|
|
16634
|
+
attention: item
|
|
16635
|
+
});
|
|
16636
|
+
previous = sequence.charAt(sequence.length - 1);
|
|
16637
|
+
}
|
|
16638
|
+
|
|
16639
|
+
return tokens
|
|
16640
|
+
}
|
|
16641
|
+
|
|
16642
|
+
/**
|
|
16643
|
+
* Serialize phrasing with attention.
|
|
16644
|
+
*
|
|
16645
|
+
* Whether attention forms depends on everything around it.
|
|
16646
|
+
* This checks that it does and tries other sequences if not.
|
|
16647
|
+
*
|
|
16648
|
+
* @param {Array<Item>} items
|
|
16649
|
+
* Phrasing items.
|
|
16650
|
+
* @param {string} before
|
|
16651
|
+
* Characters before.
|
|
16652
|
+
* @param {string} after
|
|
16653
|
+
* Characters after.
|
|
16654
|
+
* @returns {string}
|
|
16655
|
+
* Serialized markdown.
|
|
16656
|
+
*/
|
|
16657
|
+
function serialize(items, before, after) {
|
|
16658
|
+
/** @type {Map<AttentionResult, string>} */
|
|
16659
|
+
let chosen = new Map();
|
|
16660
|
+
const initial = render(items, chosen, before, after);
|
|
16661
|
+
let tokens = initial;
|
|
16662
|
+
let mistake = check(tokens, before, after);
|
|
16663
|
+
|
|
16664
|
+
while (mistake) {
|
|
16665
|
+
const next = improve(items, chosen, mistake, before, after);
|
|
16666
|
+
|
|
16667
|
+
if (!next) {
|
|
16668
|
+
tokens = initial;
|
|
16669
|
+
break
|
|
16670
|
+
}
|
|
16671
|
+
|
|
16672
|
+
chosen = next.chosen;
|
|
16673
|
+
tokens = next.tokens;
|
|
16674
|
+
mistake = next.mistake;
|
|
16675
|
+
}
|
|
16676
|
+
|
|
16677
|
+
// Join the tokens.
|
|
16678
|
+
let result = '';
|
|
16679
|
+
for (const token of tokens) result += token.value;
|
|
16680
|
+
return result
|
|
16681
|
+
}
|
|
16682
|
+
|
|
15750
16683
|
/**
|
|
15751
16684
|
* @import {AttentionInfo, Info, State} from 'mdast-util-to-markdown'
|
|
15752
16685
|
* @import {Emphasis, Parents} from 'mdast'
|
|
@@ -15774,7 +16707,8 @@ emphasis.peek = peek$1;
|
|
|
15774
16707
|
*/
|
|
15775
16708
|
function emphasis(node, _, state, info) {
|
|
15776
16709
|
const exit = state.enter('phrasing');
|
|
15777
|
-
const
|
|
16710
|
+
const item = attentionItem(node, attention$1, state, state.createTracker(info));
|
|
16711
|
+
const value = serialize([item], info.before, info.after);
|
|
15778
16712
|
exit();
|
|
15779
16713
|
return value
|
|
15780
16714
|
}
|
|
@@ -15814,18 +16748,6 @@ function peek$1(_, _1, state) {
|
|
|
15814
16748
|
return state.options.emphasis || '*'
|
|
15815
16749
|
}
|
|
15816
16750
|
|
|
15817
|
-
/**
|
|
15818
|
-
* Encode a code point as a character reference.
|
|
15819
|
-
*
|
|
15820
|
-
* @param {number} code
|
|
15821
|
-
* Code point to encode.
|
|
15822
|
-
* @returns {string}
|
|
15823
|
-
* Encoded character reference.
|
|
15824
|
-
*/
|
|
15825
|
-
function encodeCharacterReference(code) {
|
|
15826
|
-
return '&#x' + code.toString(16).toUpperCase() + ';'
|
|
15827
|
-
}
|
|
15828
|
-
|
|
15829
16751
|
/**
|
|
15830
16752
|
* @import {State} from 'mdast-util-to-markdown'
|
|
15831
16753
|
* @import {Heading} from 'mdast'
|
|
@@ -16328,21 +17250,56 @@ function link(node, _, state, info) {
|
|
|
16328
17250
|
const exit = state.enter('link');
|
|
16329
17251
|
let subexit = state.enter('label');
|
|
16330
17252
|
let value = tracker.move('[');
|
|
16331
|
-
|
|
16332
|
-
|
|
16333
|
-
|
|
16334
|
-
|
|
16335
|
-
|
|
16336
|
-
|
|
16337
|
-
);
|
|
16338
|
-
value += tracker.move('](');
|
|
17253
|
+
const text = state.containerPhrasing(node, {
|
|
17254
|
+
before: value,
|
|
17255
|
+
after: '](',
|
|
17256
|
+
...tracker.current()
|
|
17257
|
+
});
|
|
17258
|
+
value += tracker.move(text);
|
|
16339
17259
|
subexit();
|
|
17260
|
+
value += tracker.move('](');
|
|
17261
|
+
|
|
17262
|
+
// A link could accidentally turn into a definition in certain contexts:
|
|
17263
|
+
// at the start of a line, without title, when its text has a `]` that
|
|
17264
|
+
// cannot be escaped (such as in code).
|
|
17265
|
+
//
|
|
17266
|
+
// We can prevent that with an empty title:
|
|
17267
|
+
//
|
|
17268
|
+
// ```
|
|
17269
|
+
// [`a]:`](b)
|
|
17270
|
+
// ```
|
|
17271
|
+
//
|
|
17272
|
+
// …is a definition with `` `](b) `` as its destination.
|
|
17273
|
+
//
|
|
17274
|
+
// Using:
|
|
17275
|
+
//
|
|
17276
|
+
// ```
|
|
17277
|
+
// [`a]:`](b "")
|
|
17278
|
+
// ```
|
|
17279
|
+
//
|
|
17280
|
+
// …instead fixes it.
|
|
17281
|
+
//
|
|
17282
|
+
// Control characters or whitespace in the url need an enclosed destination,
|
|
17283
|
+
// which also prevents this.
|
|
17284
|
+
const enclose = /[\0- \u007F]/.test(node.url);
|
|
17285
|
+
const last = info.before.charAt(info.before.length - 1);
|
|
17286
|
+
let title = node.title || undefined;
|
|
17287
|
+
|
|
17288
|
+
if (!title && !enclose && (last === '\n' || last === '\r')) {
|
|
17289
|
+
let index = 0;
|
|
17290
|
+
|
|
17291
|
+
while (index < text.length && text.charCodeAt(index) !== 93 /* `]` */) {
|
|
17292
|
+
index += text.charCodeAt(index) === 92 /* `\` */ ? 2 : 1;
|
|
17293
|
+
}
|
|
17294
|
+
|
|
17295
|
+
if (text.charCodeAt(index + 1) === 58 /* `:` */) title = '';
|
|
17296
|
+
}
|
|
16340
17297
|
|
|
16341
17298
|
if (
|
|
16342
17299
|
// If there’s no url but there is a title…
|
|
16343
|
-
(!node.url &&
|
|
17300
|
+
(!node.url && typeof title === 'string') ||
|
|
16344
17301
|
// If there are control characters or whitespace.
|
|
16345
|
-
|
|
17302
|
+
enclose
|
|
16346
17303
|
) {
|
|
16347
17304
|
subexit = state.enter('destinationLiteral');
|
|
16348
17305
|
value += tracker.move('<');
|
|
@@ -16356,7 +17313,7 @@ function link(node, _, state, info) {
|
|
|
16356
17313
|
value += tracker.move(
|
|
16357
17314
|
state.safe(node.url, {
|
|
16358
17315
|
before: value,
|
|
16359
|
-
after:
|
|
17316
|
+
after: typeof title === 'string' ? ' ' : ')',
|
|
16360
17317
|
...tracker.current()
|
|
16361
17318
|
})
|
|
16362
17319
|
);
|
|
@@ -16364,11 +17321,11 @@ function link(node, _, state, info) {
|
|
|
16364
17321
|
|
|
16365
17322
|
subexit();
|
|
16366
17323
|
|
|
16367
|
-
if (
|
|
17324
|
+
if (typeof title === 'string') {
|
|
16368
17325
|
subexit = state.enter(`title${suffix}`);
|
|
16369
17326
|
value += tracker.move(' ' + quote);
|
|
16370
17327
|
value += tracker.move(
|
|
16371
|
-
state.safe(
|
|
17328
|
+
state.safe(title, {
|
|
16372
17329
|
before: value,
|
|
16373
17330
|
after: quote,
|
|
16374
17331
|
...tracker.current()
|
|
@@ -16852,7 +17809,7 @@ function paragraph(node, _, state, info) {
|
|
|
16852
17809
|
function root$2(node, _, state, info) {
|
|
16853
17810
|
// Note: `html` nodes are ambiguous.
|
|
16854
17811
|
const hasPhrasing = node.children.some(function (d) {
|
|
16855
|
-
return phrasing(d)
|
|
17812
|
+
return phrasing$1(d)
|
|
16856
17813
|
});
|
|
16857
17814
|
|
|
16858
17815
|
if (!hasPhrasing) return state.containerFlow(node, info)
|
|
@@ -16916,7 +17873,8 @@ strong.peek = peek;
|
|
|
16916
17873
|
*/
|
|
16917
17874
|
function strong(node, _, state, info) {
|
|
16918
17875
|
const exit = state.enter('phrasing');
|
|
16919
|
-
const
|
|
17876
|
+
const item = attentionItem(node, attention, state, state.createTracker(info));
|
|
17877
|
+
const value = serialize([item], info.before, info.after);
|
|
16920
17878
|
exit();
|
|
16921
17879
|
return value
|
|
16922
17880
|
}
|
|
@@ -16978,7 +17936,7 @@ function peek(_, _1, state) {
|
|
|
16978
17936
|
function text$3(node, parent, state, info) {
|
|
16979
17937
|
/* c8 ignore next -- parent always passed */
|
|
16980
17938
|
const siblings = parent ? parent.children : [];
|
|
16981
|
-
const index =
|
|
17939
|
+
const index = state.indexStack[state.indexStack.length - 1];
|
|
16982
17940
|
|
|
16983
17941
|
return state.safe(node.value, {
|
|
16984
17942
|
...info,
|
|
@@ -33852,21 +34810,19 @@ function getWhiteList(allowedComponents) {
|
|
|
33852
34810
|
return whitelist;
|
|
33853
34811
|
}
|
|
33854
34812
|
|
|
33855
|
-
exports.
|
|
33856
|
-
exports.asciiAlphanumeric = asciiAlphanumeric;
|
|
33857
|
-
exports.asciiPunctuation = asciiPunctuation;
|
|
34813
|
+
exports.checkRule = checkRule;
|
|
33858
34814
|
exports.classifyCharacter = classifyCharacter;
|
|
34815
|
+
exports.containerPhrasing = containerPhrasing;
|
|
33859
34816
|
exports.decodeString = decodeString;
|
|
33860
34817
|
exports.encodeCharacterReference = encodeCharacterReference;
|
|
33861
34818
|
exports.formatCodeAsIndented = formatCodeAsIndented;
|
|
33862
34819
|
exports.formatHeadingAsSetext = formatHeadingAsSetext;
|
|
33863
34820
|
exports.getLinkAttributes = getLinkAttributes;
|
|
33864
34821
|
exports.handle = handle;
|
|
33865
|
-
exports.
|
|
33866
|
-
exports.htmlRawNames = htmlRawNames;
|
|
34822
|
+
exports.htmlKind = htmlKind;
|
|
33867
34823
|
exports.markdownToHTML = markdownToHTML;
|
|
33868
34824
|
exports.patternInScope = patternInScope;
|
|
33869
|
-
exports.phrasing = phrasing;
|
|
34825
|
+
exports.phrasing = phrasing$1;
|
|
33870
34826
|
exports.remarkGfm = remarkGfm;
|
|
33871
34827
|
exports.remarkParse = remarkParse;
|
|
33872
34828
|
exports.sanitizeHTML = sanitizeHTML;
|