@sveltia/ui 0.87.1 → 0.87.3

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.
@@ -6,12 +6,10 @@ import {
6
6
  BOLD_ITALIC_UNDERSCORE,
7
7
  BOLD_STAR,
8
8
  BOLD_UNDERSCORE,
9
- CODE,
10
9
  HEADING,
11
10
  INLINE_CODE,
12
11
  ITALIC_STAR,
13
12
  ITALIC_UNDERSCORE,
14
- LINK,
15
13
  ORDERED_LIST,
16
14
  QUOTE,
17
15
  STRIKETHROUGH,
@@ -20,6 +18,10 @@ import {
20
18
  import { HeadingNode, QuoteNode } from '@lexical/rich-text';
21
19
  import { createCommand } from 'lexical';
22
20
 
21
+ import { CODE } from './transformers/code.js';
22
+ import { LEADING_INLINE_CODE } from './transformers/inline-code.js';
23
+ import { LINK } from './transformers/link.js';
24
+
23
25
  /**
24
26
  * @import { EditorThemeClasses, LexicalCommand } from 'lexical';
25
27
  * @import { Transformer } from '@lexical/markdown';
@@ -219,7 +221,8 @@ export const TRANSFORMER_MAP = {
219
221
  // MULTILINE_ELEMENT_TRANSFORMERS
220
222
  'code-block': [CODE],
221
223
  // TEXT_FORMAT_TRANSFORMERS
222
- code: [INLINE_CODE],
224
+ // The second one keeps the backslashes in inline code at the start of a node
225
+ code: [INLINE_CODE, LEADING_INLINE_CODE],
223
226
  bold: [
224
227
  BOLD_STAR,
225
228
  // Disabled for Markdown output in `DISABLED_MARKDOWN_TAGS` below
@@ -236,6 +239,7 @@ export const TRANSFORMER_MAP = {
236
239
  ],
237
240
  strikethrough: [STRIKETHROUGH],
238
241
  // TEXT_MATCH_TRANSFORMERS
242
+ // Unlike Lexical’s, it keeps the formats of the whole link text outside the link
239
243
  link: [LINK],
240
244
  };
241
245
 
@@ -30,7 +30,6 @@ import {
30
30
  ListNode,
31
31
  } from '@lexical/list';
32
32
  import {
33
- CODE,
34
33
  HEADING,
35
34
  $convertFromMarkdownString as convertFromMarkdownString,
36
35
  $convertToMarkdownString as convertToMarkdownString,
@@ -94,10 +93,11 @@ import {
94
93
  HTML_IMPORT_MAP,
95
94
  } from './html.js';
96
95
  import {
96
+ addImportPlaceholders,
97
97
  convertSetextHeadings,
98
98
  convertTildeFences,
99
- increaseListIndentation,
100
99
  padBlankBlockquoteLines,
100
+ removeImportPlaceholders,
101
101
  splitMultilineFormatting,
102
102
  trimBlankBlockquoteLines,
103
103
  } from './markdown.js';
@@ -111,7 +111,10 @@ import {
111
111
  import { registerCodeHighlighting, shikiTokenizer } from './shiki/highlighter.js';
112
112
  import { getCodeTheme, observeCodeTheme } from './shiki/theme.js';
113
113
  import { BLOCK_SYNTAX_ESCAPE } from './transformers/block-escape.js';
114
+ import { CODE } from './transformers/code.js';
115
+ import { addCodeSpanSplit } from './transformers/formatted-code.js';
114
116
  import { HR } from './transformers/hr.js';
117
+ import { RAW_SYNTAX } from './transformers/raw-syntax.js';
115
118
  import { $getSelectedAlignment } from './alignment.js';
116
119
  import {
117
120
  $ensureParagraphAfterTable,
@@ -999,6 +1002,8 @@ export const initEditor = ({
999
1002
  componentTransformers.filter(({ type }) => type === 'text-match')
1000
1003
  ),
1001
1004
  ),
1005
+ // Last, so the syntax of the editor’s own comes first
1006
+ RAW_SYNTAX,
1002
1007
  ]),
1003
1008
  ];
1004
1009
 
@@ -1155,6 +1160,37 @@ const importValue = async (editor, { languages, $import, $export, formatName })
1155
1160
  return editor.read($export);
1156
1161
  };
1157
1162
 
1163
+ /**
1164
+ * Remove the placeholders added by `addImportPlaceholders()` from the imported nodes: the text, the
1165
+ * Markdown of raw syntax, and the URL and title of a link.
1166
+ */
1167
+ const removePlaceholders = () => {
1168
+ getRoot()
1169
+ .getAllTextNodes()
1170
+ .forEach((node) => {
1171
+ const text = node.getTextContent();
1172
+ const newText = removeImportPlaceholders(text);
1173
+ const link = node.getParent();
1174
+
1175
+ if (newText !== text) {
1176
+ node.setTextContent(newText);
1177
+ }
1178
+
1179
+ if (isLinkNode(link)) {
1180
+ const url = link.getURL();
1181
+ const title = link.getTitle();
1182
+
1183
+ if (removeImportPlaceholders(url) !== url) {
1184
+ link.setURL(removeImportPlaceholders(url));
1185
+ }
1186
+
1187
+ if (title && removeImportPlaceholders(title) !== title) {
1188
+ link.setTitle(removeImportPlaceholders(title));
1189
+ }
1190
+ }
1191
+ });
1192
+ };
1193
+
1158
1194
  /**
1159
1195
  * Convert Markdown to Lexical nodes.
1160
1196
  * @param {LexicalEditor} editor Editor instance.
@@ -1183,20 +1219,30 @@ export const convertMarkdownToLexical = async (editor, value, enabledTransformer
1183
1219
  const normalizedValue = hasHeadings ? convertSetextHeadings(fencedValue) : fencedValue;
1184
1220
 
1185
1221
  return importValue(editor, {
1186
- languages: [...normalizedValue.matchAll(/^`{3,}(?<lang>[^`\n]+)\n/gm)].map(
1222
+ // The language is the leading word of the info string, like `js` in ```` ``` js title=x ````;
1223
+ // see `CODE`
1224
+ languages: [...normalizedValue.matchAll(/^`{3,}[ \t]*(?<lang>[\w-]+)/gm)].map(
1187
1225
  ({ groups: { lang = 'plain' } = {} }) => lang,
1188
1226
  ),
1189
1227
  /**
1190
1228
  * Import the Markdown.
1191
1229
  */
1192
1230
  $import: () => {
1231
+ // Split multiline formatting into separate lines to prevent Markdown parsing issues
1232
+ const splitValue = splitMultilineFormatting(normalizedValue);
1233
+ // Add placeholders so Lexical reads the backslashes and the tabs in code spans right
1234
+ const markedValue = addImportPlaceholders(splitValue);
1235
+
1193
1236
  convertFromMarkdownString(
1194
- // Split multiline formatting into separate lines to prevent Markdown parsing issues, then
1195
- // increase list indentation levels to prevent Markdown parsing issues, and pad blank
1196
- // blockquote lines so they are not imported as literal `>` text
1197
- padBlankBlockquoteLines(increaseListIndentation(splitMultilineFormatting(normalizedValue))),
1198
- enabledTransformers,
1237
+ // Pad blank blockquote lines so they are not imported as literal `>` text
1238
+ padBlankBlockquoteLines(markedValue),
1239
+ // Keep the formatting around a code span after other formatting
1240
+ addCodeSpanSplit(enabledTransformers),
1199
1241
  );
1242
+
1243
+ if (markedValue !== splitValue) {
1244
+ removePlaceholders();
1245
+ }
1200
1246
  },
1201
1247
  /**
1202
1248
  * Export the content as Markdown.
@@ -1,7 +1,9 @@
1
1
  export function splitMultilineFormatting(value: string): string;
2
+ export function findCodeSpans(line: string): CodeSpan[];
3
+ export function addImportPlaceholders(value: string): string;
4
+ export function removeImportPlaceholders(text: string): string;
2
5
  export function convertTildeFences(value: string): string;
3
6
  export function convertSetextHeadings(value: string): string;
4
- export function increaseListIndentation(value: string): string;
5
7
  export function padBlankBlockquoteLines(value: string): string;
6
8
  export function trimBlankBlockquoteLines(value: string): string;
7
9
  export type CodeBlock = {
@@ -23,3 +25,32 @@ export type CodeBlock = {
23
25
  */
24
26
  closed: boolean;
25
27
  };
28
+ export type FormattedLine = {
29
+ /**
30
+ * Whitespace at the start of the line, outside the formatting.
31
+ */
32
+ indent: string;
33
+ /**
34
+ * Text of the line within the formatting.
35
+ */
36
+ text: string;
37
+ /**
38
+ * Whitespace or the backslash of a hard line break at the end of the
39
+ * line, outside the formatting.
40
+ */
41
+ lineEnd: string;
42
+ };
43
+ export type CodeSpan = {
44
+ /**
45
+ * Index of the opening backticks.
46
+ */
47
+ start: number;
48
+ /**
49
+ * Index after the closing backticks.
50
+ */
51
+ end: number;
52
+ /**
53
+ * Number of backticks on either side.
54
+ */
55
+ fenceLength: number;
56
+ };