@lobehub/ui 2.10.0 → 2.10.2
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/es/Markdown/Markdown.js +21 -8
- package/es/Markdown/SyntaxMarkdown/StreamdownRender.js +5 -9
- package/es/Markdown/type.d.ts +2 -0
- package/es/hooks/useMarkdown/useMarkdownRehypePlugins.js +3 -2
- package/package.json +1 -1
- package/es/Markdown/SyntaxMarkdown/parseIncompleteMarkdown.d.ts +0 -1
- package/es/Markdown/SyntaxMarkdown/parseIncompleteMarkdown.js +0 -293
package/es/Markdown/Markdown.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
3
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
4
|
-
var _excluded = ["ref", "children", "className", "style", "fullFeaturedCodeBlock", "onDoubleClick", "animated", "enableLatex", "enableMermaid", "enableImageGallery", "enableCustomFootnotes", "componentProps", "allowHtml", "fontSize", "headerMultiple", "marginMultiple", "showFootnotes", "variant", "reactMarkdownProps", "lineHeight", "rehypePlugins", "remarkPlugins", "remarkPluginsAhead", "components", "customRender", "citations"]
|
|
4
|
+
var _excluded = ["ref", "children", "className", "style", "fullFeaturedCodeBlock", "onDoubleClick", "animated", "enableLatex", "enableMermaid", "enableImageGallery", "enableCustomFootnotes", "enableGithubAlert", "enableStream", "componentProps", "allowHtml", "fontSize", "headerMultiple", "marginMultiple", "showFootnotes", "variant", "reactMarkdownProps", "lineHeight", "rehypePlugins", "remarkPlugins", "remarkPluginsAhead", "components", "customRender", "citations"],
|
|
5
|
+
_excluded2 = ["children"];
|
|
5
6
|
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
6
7
|
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
7
8
|
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
@@ -16,7 +17,7 @@ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
|
16
17
|
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
17
18
|
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
18
19
|
import { cva } from 'class-variance-authority';
|
|
19
|
-
import { memo, useEffect, useMemo, useState } from 'react';
|
|
20
|
+
import { memo, useCallback, useEffect, useMemo, useState } from 'react';
|
|
20
21
|
import { PreviewGroup } from "../Image";
|
|
21
22
|
import { MarkdownProvider } from "./components/MarkdownProvider";
|
|
22
23
|
import { MarkdownRender, StreamdownRender } from "./SyntaxMarkdown";
|
|
@@ -31,12 +32,12 @@ var Markdown = /*#__PURE__*/memo(function (_ref) {
|
|
|
31
32
|
fullFeaturedCodeBlock = _ref.fullFeaturedCodeBlock,
|
|
32
33
|
onDoubleClick = _ref.onDoubleClick,
|
|
33
34
|
animated = _ref.animated,
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
_ref$enableMermaid = _ref.enableMermaid,
|
|
37
|
-
enableMermaid = _ref$enableMermaid === void 0 ? true : _ref$enableMermaid,
|
|
35
|
+
eLatex = _ref.enableLatex,
|
|
36
|
+
eMermaid = _ref.enableMermaid,
|
|
38
37
|
enableImageGallery = _ref.enableImageGallery,
|
|
39
38
|
enableCustomFootnotes = _ref.enableCustomFootnotes,
|
|
39
|
+
eGithubAlert = _ref.enableGithubAlert,
|
|
40
|
+
eStream = _ref.enableStream,
|
|
40
41
|
componentProps = _ref.componentProps,
|
|
41
42
|
allowHtml = _ref.allowHtml,
|
|
42
43
|
_ref$fontSize = _ref.fontSize,
|
|
@@ -61,11 +62,14 @@ var Markdown = /*#__PURE__*/memo(function (_ref) {
|
|
|
61
62
|
var _useStyles = useStyles(),
|
|
62
63
|
cx = _useStyles.cx,
|
|
63
64
|
styles = _useStyles.styles;
|
|
64
|
-
// Add state to track delayed animated value
|
|
65
65
|
var _useState = useState(animated),
|
|
66
66
|
_useState2 = _slicedToArray(_useState, 2),
|
|
67
67
|
delayedAnimated = _useState2[0],
|
|
68
68
|
setDelayedAnimated = _useState2[1];
|
|
69
|
+
var enableLatex = Boolean(typeof eLatex === 'boolean' && eLatex) || children.includes('$');
|
|
70
|
+
var enableMermaid = Boolean(typeof eMermaid === 'boolean' && eMermaid) || children.includes('```mermaid');
|
|
71
|
+
var enableGithubAlert = Boolean(typeof eGithubAlert === 'boolean' && eGithubAlert) || children.includes('> [!');
|
|
72
|
+
var enableStream = Boolean(typeof eStream === 'boolean' && eStream) || delayedAnimated || !children.includes('[^');
|
|
69
73
|
|
|
70
74
|
// Watch for changes in animated prop
|
|
71
75
|
useEffect(function () {
|
|
@@ -109,7 +113,15 @@ var Markdown = /*#__PURE__*/memo(function (_ref) {
|
|
|
109
113
|
/* eslint-enable sort-keys-fix/sort-keys-fix */
|
|
110
114
|
});
|
|
111
115
|
}, [styles]);
|
|
112
|
-
var DefaultRender =
|
|
116
|
+
var DefaultRender = useCallback(function (_ref2) {
|
|
117
|
+
var children = _ref2.children,
|
|
118
|
+
reactMarkdownProps = _objectWithoutProperties(_ref2, _excluded2);
|
|
119
|
+
return enableStream ? /*#__PURE__*/_jsx(StreamdownRender, _objectSpread(_objectSpread({}, reactMarkdownProps), {}, {
|
|
120
|
+
children: children
|
|
121
|
+
})) : /*#__PURE__*/_jsx(MarkdownRender, _objectSpread(_objectSpread({}, reactMarkdownProps), {}, {
|
|
122
|
+
children: children
|
|
123
|
+
}));
|
|
124
|
+
}, [enableStream]);
|
|
113
125
|
var defaultDOM = /*#__PURE__*/_jsx(DefaultRender, _objectSpread(_objectSpread({}, reactMarkdownProps), {}, {
|
|
114
126
|
children: children
|
|
115
127
|
}));
|
|
@@ -138,6 +150,7 @@ var Markdown = /*#__PURE__*/memo(function (_ref) {
|
|
|
138
150
|
componentProps: componentProps,
|
|
139
151
|
components: components,
|
|
140
152
|
enableCustomFootnotes: enableCustomFootnotes,
|
|
153
|
+
enableGithubAlert: enableGithubAlert,
|
|
141
154
|
enableLatex: enableLatex,
|
|
142
155
|
enableMermaid: enableMermaid,
|
|
143
156
|
fullFeaturedCodeBlock: fullFeaturedCodeBlock,
|
|
@@ -14,8 +14,8 @@ import { marked } from 'marked';
|
|
|
14
14
|
import { memo, useId, useMemo } from 'react';
|
|
15
15
|
import { MarkdownHooks } from 'react-markdown';
|
|
16
16
|
import { useMarkdownComponents, useMarkdownContent, useMarkdownRehypePlugins, useMarkdownRemarkPlugins } from "../../hooks/useMarkdown";
|
|
17
|
-
import { parseIncompleteMarkdown } from "./parseIncompleteMarkdown";
|
|
18
17
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
18
|
+
import { createElement as _createElement } from "react";
|
|
19
19
|
var parseMarkdownIntoBlocks = function parseMarkdownIntoBlocks(markdown) {
|
|
20
20
|
var tokens = marked.lexer(markdown);
|
|
21
21
|
return tokens.map(function (token) {
|
|
@@ -25,11 +25,8 @@ var parseMarkdownIntoBlocks = function parseMarkdownIntoBlocks(markdown) {
|
|
|
25
25
|
var StreamdownBlock = /*#__PURE__*/memo(function (_ref) {
|
|
26
26
|
var children = _ref.children,
|
|
27
27
|
rest = _objectWithoutProperties(_ref, _excluded);
|
|
28
|
-
var parsedContent = useMemo(function () {
|
|
29
|
-
return typeof children === 'string' ? parseIncompleteMarkdown(children.trim()) : children;
|
|
30
|
-
}, [children]);
|
|
31
28
|
return /*#__PURE__*/_jsx(MarkdownHooks, _objectSpread(_objectSpread({}, rest), {}, {
|
|
32
|
-
children:
|
|
29
|
+
children: children
|
|
33
30
|
}));
|
|
34
31
|
}, function (prevProps, nextProps) {
|
|
35
32
|
return prevProps.children === nextProps.children;
|
|
@@ -48,13 +45,12 @@ export var StreamdownRender = /*#__PURE__*/memo(function (_ref2) {
|
|
|
48
45
|
}, [escapedContent]);
|
|
49
46
|
return /*#__PURE__*/_jsx("div", {
|
|
50
47
|
children: blocks.map(function (block, index) {
|
|
51
|
-
return /*#__PURE__*/
|
|
48
|
+
return /*#__PURE__*/_createElement(StreamdownBlock, _objectSpread(_objectSpread({}, rest), {}, {
|
|
52
49
|
components: components,
|
|
50
|
+
key: "".concat(generatedId, "-block_").concat(index),
|
|
53
51
|
rehypePlugins: rehypePluginsList,
|
|
54
52
|
remarkPlugins: remarkPluginsList
|
|
55
|
-
},
|
|
56
|
-
children: block
|
|
57
|
-
}), "".concat(generatedId, "-block_").concat(index));
|
|
53
|
+
}), block);
|
|
58
54
|
})
|
|
59
55
|
});
|
|
60
56
|
}, function (prevProps, nextProps) {
|
package/es/Markdown/type.d.ts
CHANGED
|
@@ -30,8 +30,10 @@ export interface SyntaxMarkdownProps {
|
|
|
30
30
|
};
|
|
31
31
|
components?: Components & Record<string, FC>;
|
|
32
32
|
enableCustomFootnotes?: boolean;
|
|
33
|
+
enableGithubAlert?: boolean;
|
|
33
34
|
enableLatex?: boolean;
|
|
34
35
|
enableMermaid?: boolean;
|
|
36
|
+
enableStream?: boolean;
|
|
35
37
|
fullFeaturedCodeBlock?: boolean;
|
|
36
38
|
reactMarkdownProps?: Omit<Readonly<ReactMarkdownOptions>, 'components' | 'rehypePlugins' | 'remarkPlugins'>;
|
|
37
39
|
rehypePlugins?: Pluggable[];
|
|
@@ -19,14 +19,15 @@ export var useMarkdownRehypePlugins = function useMarkdownRehypePlugins() {
|
|
|
19
19
|
animated = _useMarkdownContext.animated,
|
|
20
20
|
enableLatex = _useMarkdownContext.enableLatex,
|
|
21
21
|
enableCustomFootnotes = _useMarkdownContext.enableCustomFootnotes,
|
|
22
|
+
enableGithubAlert = _useMarkdownContext.enableGithubAlert,
|
|
22
23
|
allowHtml = _useMarkdownContext.allowHtml,
|
|
23
24
|
_useMarkdownContext$r = _useMarkdownContext.rehypePlugins,
|
|
24
25
|
rehypePlugins = _useMarkdownContext$r === void 0 ? [] : _useMarkdownContext$r,
|
|
25
26
|
_useMarkdownContext$r2 = _useMarkdownContext.rehypePluginsAhead,
|
|
26
27
|
rehypePluginsAhead = _useMarkdownContext$r2 === void 0 ? [] : _useMarkdownContext$r2;
|
|
27
28
|
var memoPlugins = useMemo(function () {
|
|
28
|
-
return [
|
|
29
|
-
}, [animated, enableLatex, enableCustomFootnotes, allowHtml]);
|
|
29
|
+
return [allowHtml && rehypeRaw, enableGithubAlert && rehypeGithubAlerts, enableLatex && rehypeKatex, enableLatex && rehypeKatexDir, enableCustomFootnotes && rehypeFootnoteLinks, animated && animatedPlugin].filter(Boolean);
|
|
30
|
+
}, [animated, enableLatex, enableGithubAlert, enableCustomFootnotes, allowHtml]);
|
|
30
31
|
return useMemo(function () {
|
|
31
32
|
return [].concat(_toConsumableArray(rehypePluginsAhead), _toConsumableArray(memoPlugins), _toConsumableArray(rehypePlugins));
|
|
32
33
|
}, [rehypePlugins, memoPlugins, rehypePluginsAhead]);
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const parseIncompleteMarkdown: (text: string) => string;
|
|
@@ -1,293 +0,0 @@
|
|
|
1
|
-
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
|
|
2
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
3
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
4
|
-
var linkImagePattern = /(!?\[)([^\]]*?)$/;
|
|
5
|
-
var boldPattern = /(\*\*)([^*]*?)$/;
|
|
6
|
-
var italicPattern = /(__)([^_]*?)$/;
|
|
7
|
-
var boldItalicPattern = /(\*{3})([^*]*?)$/;
|
|
8
|
-
var singleAsteriskPattern = /(\*)([^*]*?)$/;
|
|
9
|
-
var singleUnderscorePattern = /(_)([^_]*?)$/;
|
|
10
|
-
var inlineCodePattern = /(`)([^`]*?)$/;
|
|
11
|
-
var strikethroughPattern = /(~~)([^~]*?)$/;
|
|
12
|
-
var inlineKatexPattern = /(\$)([^$]*?)$/;
|
|
13
|
-
var blockKatexPattern = /(\$\$)([^$]*?)$/;
|
|
14
|
-
|
|
15
|
-
// Handles incomplete links and images by removing them if not closed
|
|
16
|
-
var handleIncompleteLinksAndImages = function handleIncompleteLinksAndImages(text) {
|
|
17
|
-
var linkMatch = text.match(linkImagePattern);
|
|
18
|
-
if (linkMatch) {
|
|
19
|
-
var startIndex = text.lastIndexOf(linkMatch[1]);
|
|
20
|
-
return text.slice(0, Math.max(0, startIndex));
|
|
21
|
-
}
|
|
22
|
-
return text;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
// Completes incomplete bold formatting (**)
|
|
26
|
-
var handleIncompleteBold = function handleIncompleteBold(text) {
|
|
27
|
-
var boldMatch = text.match(boldPattern);
|
|
28
|
-
if (boldMatch) {
|
|
29
|
-
var asteriskPairs = (text.match(/\*\*/g) || []).length;
|
|
30
|
-
if (asteriskPairs % 2 === 1) {
|
|
31
|
-
return "".concat(text, "**");
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
return text;
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
// Completes incomplete italic formatting with double underscores (__)
|
|
38
|
-
var handleIncompleteDoubleUnderscoreItalic = function handleIncompleteDoubleUnderscoreItalic(text) {
|
|
39
|
-
var italicMatch = text.match(italicPattern);
|
|
40
|
-
if (italicMatch) {
|
|
41
|
-
var underscorePairs = (text.match(/__/g) || []).length;
|
|
42
|
-
if (underscorePairs % 2 === 1) {
|
|
43
|
-
return "".concat(text, "__");
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return text;
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
// Counts single asterisks that are not part of double asterisks and not escaped
|
|
50
|
-
var countSingleAsterisks = function countSingleAsterisks(text) {
|
|
51
|
-
return text.split('').reduce(function (acc, char, index) {
|
|
52
|
-
if (char === '*') {
|
|
53
|
-
var prevChar = text[index - 1];
|
|
54
|
-
var nextChar = text[index + 1];
|
|
55
|
-
// Skip if escaped with backslash
|
|
56
|
-
if (prevChar === '\\') {
|
|
57
|
-
return acc;
|
|
58
|
-
}
|
|
59
|
-
if (prevChar !== '*' && nextChar !== '*') {
|
|
60
|
-
return acc + 1;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
return acc;
|
|
64
|
-
}, 0);
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
// Completes incomplete italic formatting with single asterisks (*)
|
|
68
|
-
var handleIncompleteSingleAsteriskItalic = function handleIncompleteSingleAsteriskItalic(text) {
|
|
69
|
-
var singleAsteriskMatch = text.match(singleAsteriskPattern);
|
|
70
|
-
if (singleAsteriskMatch) {
|
|
71
|
-
var singleAsterisks = countSingleAsterisks(text);
|
|
72
|
-
if (singleAsterisks % 2 === 1) {
|
|
73
|
-
return "".concat(text, "*");
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
return text;
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
// Counts single underscores that are not part of double underscores and not escaped
|
|
80
|
-
var countSingleUnderscores = function countSingleUnderscores(text) {
|
|
81
|
-
return text.split('').reduce(function (acc, char, index) {
|
|
82
|
-
if (char === '_') {
|
|
83
|
-
var prevChar = text[index - 1];
|
|
84
|
-
var nextChar = text[index + 1];
|
|
85
|
-
// Skip if escaped with backslash
|
|
86
|
-
if (prevChar === '\\') {
|
|
87
|
-
return acc;
|
|
88
|
-
}
|
|
89
|
-
if (prevChar !== '_' && nextChar !== '_') {
|
|
90
|
-
return acc + 1;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
return acc;
|
|
94
|
-
}, 0);
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
// Completes incomplete italic formatting with single underscores (_)
|
|
98
|
-
var handleIncompleteSingleUnderscoreItalic = function handleIncompleteSingleUnderscoreItalic(text) {
|
|
99
|
-
var singleUnderscoreMatch = text.match(singleUnderscorePattern);
|
|
100
|
-
if (singleUnderscoreMatch) {
|
|
101
|
-
var singleUnderscores = countSingleUnderscores(text);
|
|
102
|
-
if (singleUnderscores % 2 === 1) {
|
|
103
|
-
return "".concat(text, "_");
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
return text;
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
// Checks if a backtick at position i is part of a triple backtick sequence
|
|
110
|
-
var isPartOfTripleBacktick = function isPartOfTripleBacktick(text, i) {
|
|
111
|
-
// eslint-disable-next-line unicorn/prefer-string-slice
|
|
112
|
-
var isTripleStart = text.substring(i, i + 3) === '```';
|
|
113
|
-
// eslint-disable-next-line unicorn/prefer-string-slice
|
|
114
|
-
var isTripleMiddle = i > 0 && text.substring(i - 1, i + 2) === '```';
|
|
115
|
-
// eslint-disable-next-line unicorn/prefer-string-slice
|
|
116
|
-
var isTripleEnd = i > 1 && text.substring(i - 2, i + 1) === '```';
|
|
117
|
-
return isTripleStart || isTripleMiddle || isTripleEnd;
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
// Counts single backticks that are not part of triple backticks
|
|
121
|
-
var countSingleBackticks = function countSingleBackticks(text) {
|
|
122
|
-
var count = 0;
|
|
123
|
-
for (var i = 0; i < text.length; i++) {
|
|
124
|
-
if (text[i] === '`' && !isPartOfTripleBacktick(text, i)) {
|
|
125
|
-
count++;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
return count;
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
// Completes incomplete inline code formatting (`)
|
|
132
|
-
// Avoids completing if inside an incomplete code block
|
|
133
|
-
var handleIncompleteInlineCode = function handleIncompleteInlineCode(text) {
|
|
134
|
-
// Check if we have inline triple backticks (starts with ``` and should end with ```)
|
|
135
|
-
// This pattern should ONLY match truly inline code (no newlines)
|
|
136
|
-
// Examples: ```code``` or ```python code```
|
|
137
|
-
var inlineTripleBacktickMatch = text.match(/^```[^\n`]*```?$/);
|
|
138
|
-
if (inlineTripleBacktickMatch && !text.includes('\n')) {
|
|
139
|
-
// Check if it ends with exactly 2 backticks (incomplete)
|
|
140
|
-
if (text.endsWith('``') && !text.endsWith('```')) {
|
|
141
|
-
return "".concat(text) + '`';
|
|
142
|
-
}
|
|
143
|
-
// Already complete inline triple backticks
|
|
144
|
-
return text;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
// Check if we're inside a multi-line code block (complete or incomplete)
|
|
148
|
-
var allTripleBackticks = (text.match(/```/g) || []).length;
|
|
149
|
-
var insideIncompleteCodeBlock = allTripleBackticks % 2 === 1;
|
|
150
|
-
|
|
151
|
-
// Special case: if text ends with ```\n (triple backticks followed by newline)
|
|
152
|
-
// This is actually a complete code block, not incomplete
|
|
153
|
-
if ((text.endsWith('```\n') || text.endsWith('```')) &&
|
|
154
|
-
// Count all triple backticks - if even, it's complete
|
|
155
|
-
allTripleBackticks % 2 === 0) {
|
|
156
|
-
return text;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// Don't modify text if we have complete multi-line code blocks (even pairs of ```)
|
|
160
|
-
if (allTripleBackticks > 0 && allTripleBackticks % 2 === 0 && text.includes('\n')) {
|
|
161
|
-
// We have complete multi-line code blocks, don't add any backticks
|
|
162
|
-
return text;
|
|
163
|
-
}
|
|
164
|
-
var inlineCodeMatch = text.match(inlineCodePattern);
|
|
165
|
-
if (inlineCodeMatch && !insideIncompleteCodeBlock) {
|
|
166
|
-
var singleBacktickCount = countSingleBackticks(text);
|
|
167
|
-
if (singleBacktickCount % 2 === 1) {
|
|
168
|
-
return "".concat(text) + '`';
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
return text;
|
|
172
|
-
};
|
|
173
|
-
|
|
174
|
-
// Completes incomplete strikethrough formatting (~~)
|
|
175
|
-
var handleIncompleteStrikethrough = function handleIncompleteStrikethrough(text) {
|
|
176
|
-
var strikethroughMatch = text.match(strikethroughPattern);
|
|
177
|
-
if (strikethroughMatch) {
|
|
178
|
-
var tildePairs = (text.match(/~~/g) || []).length;
|
|
179
|
-
if (tildePairs % 2 === 1) {
|
|
180
|
-
return "".concat(text, "~~");
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
return text;
|
|
184
|
-
};
|
|
185
|
-
|
|
186
|
-
// Counts single dollar signs that are not part of double dollar signs and not escaped
|
|
187
|
-
var countSingleDollarSigns = function countSingleDollarSigns(text) {
|
|
188
|
-
return text.split('').reduce(function (acc, char, index) {
|
|
189
|
-
if (char === '$') {
|
|
190
|
-
var prevChar = text[index - 1];
|
|
191
|
-
var nextChar = text[index + 1];
|
|
192
|
-
// Skip if escaped with backslash
|
|
193
|
-
if (prevChar === '\\') {
|
|
194
|
-
return acc;
|
|
195
|
-
}
|
|
196
|
-
if (prevChar !== '$' && nextChar !== '$') {
|
|
197
|
-
return acc + 1;
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
return acc;
|
|
201
|
-
}, 0);
|
|
202
|
-
};
|
|
203
|
-
|
|
204
|
-
// Completes incomplete block KaTeX formatting ($$)
|
|
205
|
-
var handleIncompleteBlockKatex = function handleIncompleteBlockKatex(text) {
|
|
206
|
-
var blockKatexMatch = text.match(blockKatexPattern);
|
|
207
|
-
if (blockKatexMatch) {
|
|
208
|
-
var dollarPairs = (text.match(/\$\$/g) || []).length;
|
|
209
|
-
if (dollarPairs % 2 === 1) {
|
|
210
|
-
return "".concat(text, "$$");
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
return text;
|
|
214
|
-
};
|
|
215
|
-
|
|
216
|
-
// Completes incomplete inline KaTeX formatting ($)
|
|
217
|
-
var handleIncompleteInlineKatex = function handleIncompleteInlineKatex(text) {
|
|
218
|
-
var inlineKatexMatch = text.match(inlineKatexPattern);
|
|
219
|
-
if (inlineKatexMatch) {
|
|
220
|
-
var singleDollars = countSingleDollarSigns(text);
|
|
221
|
-
if (singleDollars % 2 === 1) {
|
|
222
|
-
return "".concat(text, "$");
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
return text;
|
|
226
|
-
};
|
|
227
|
-
|
|
228
|
-
// Counts triple asterisks that are not part of quadruple or more asterisks
|
|
229
|
-
var countTripleAsterisks = function countTripleAsterisks(text) {
|
|
230
|
-
var count = 0;
|
|
231
|
-
var matches = text.match(/\*+/g) || [];
|
|
232
|
-
var _iterator = _createForOfIteratorHelper(matches),
|
|
233
|
-
_step;
|
|
234
|
-
try {
|
|
235
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
236
|
-
var match = _step.value;
|
|
237
|
-
// Count how many complete triple asterisks are in this sequence
|
|
238
|
-
var asteriskCount = match.length;
|
|
239
|
-
if (asteriskCount >= 3) {
|
|
240
|
-
// Each group of exactly 3 asterisks counts as one triple asterisk marker
|
|
241
|
-
count += Math.floor(asteriskCount / 3);
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
} catch (err) {
|
|
245
|
-
_iterator.e(err);
|
|
246
|
-
} finally {
|
|
247
|
-
_iterator.f();
|
|
248
|
-
}
|
|
249
|
-
return count;
|
|
250
|
-
};
|
|
251
|
-
|
|
252
|
-
// Completes incomplete bold-italic formatting (***)
|
|
253
|
-
var handleIncompleteBoldItalic = function handleIncompleteBoldItalic(text) {
|
|
254
|
-
// Don't process if text is only asterisks and has 4 or more consecutive asterisks
|
|
255
|
-
// This prevents cases like **** from being treated as incomplete ***
|
|
256
|
-
if (/^\*{4,}$/.test(text)) {
|
|
257
|
-
return text;
|
|
258
|
-
}
|
|
259
|
-
var boldItalicMatch = text.match(boldItalicPattern);
|
|
260
|
-
if (boldItalicMatch) {
|
|
261
|
-
var tripleAsteriskCount = countTripleAsterisks(text);
|
|
262
|
-
if (tripleAsteriskCount % 2 === 1) {
|
|
263
|
-
return "".concat(text, "***");
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
return text;
|
|
267
|
-
};
|
|
268
|
-
|
|
269
|
-
// Parses markdown text and removes incomplete tokens to prevent partial rendering
|
|
270
|
-
export var parseIncompleteMarkdown = function parseIncompleteMarkdown(text) {
|
|
271
|
-
if (!text || typeof text !== 'string') {
|
|
272
|
-
return text;
|
|
273
|
-
}
|
|
274
|
-
var result = text;
|
|
275
|
-
|
|
276
|
-
// Handle incomplete links and images first (removes content)
|
|
277
|
-
result = handleIncompleteLinksAndImages(result);
|
|
278
|
-
|
|
279
|
-
// Handle various formatting completions
|
|
280
|
-
// Handle triple asterisks first (most specific)
|
|
281
|
-
result = handleIncompleteBoldItalic(result);
|
|
282
|
-
result = handleIncompleteBold(result);
|
|
283
|
-
result = handleIncompleteDoubleUnderscoreItalic(result);
|
|
284
|
-
result = handleIncompleteSingleAsteriskItalic(result);
|
|
285
|
-
result = handleIncompleteSingleUnderscoreItalic(result);
|
|
286
|
-
result = handleIncompleteInlineCode(result);
|
|
287
|
-
result = handleIncompleteStrikethrough(result);
|
|
288
|
-
|
|
289
|
-
// Handle KaTeX formatting (block first, then inline)
|
|
290
|
-
result = handleIncompleteBlockKatex(result);
|
|
291
|
-
result = handleIncompleteInlineKatex(result);
|
|
292
|
-
return result;
|
|
293
|
-
};
|