@sveltia/ui 0.79.1 → 0.79.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.
@@ -86,6 +86,7 @@ import {
86
86
  } from './shiki/facade.js';
87
87
  import { registerCodeHighlighting, shikiTokenizer } from './shiki/highlighter.js';
88
88
  import { getCodeTheme, observeCodeTheme } from './shiki/theme.js';
89
+ import { BLOCK_SYNTAX_ESCAPE } from './transformers/block-escape.js';
89
90
  import { HR } from './transformers/hr.js';
90
91
  import { TABLE } from './transformers/table.js';
91
92
 
@@ -455,9 +456,13 @@ export const getSelectionTypes = () => {
455
456
  * @returns {string} Markdown value.
456
457
  */
457
458
  export const exportMarkdown = (enabledTransformers) => {
458
- const transformers = enabledTransformers.filter(
459
- (/** @type {any} */ { tag }) => !DISABLED_MARKDOWN_TAGS.includes(tag),
460
- );
459
+ const transformers = [
460
+ ...enabledTransformers.filter(
461
+ (/** @type {any} */ { tag }) => !DISABLED_MARKDOWN_TAGS.includes(tag),
462
+ ),
463
+ // Last, so a transformer of the editor’s own, such as a link, comes first
464
+ BLOCK_SYNTAX_ESCAPE,
465
+ ];
461
466
 
462
467
  return trimBlankBlockquoteLines(
463
468
  convertToMarkdownString(transformers)
@@ -1,4 +1,4 @@
1
1
  /**
2
2
  * Version of this package, used to resolve the prebuilt Shiki engine chunk from a CDN.
3
3
  */
4
- export const UI_VERSION: "0.79.1";
4
+ export const UI_VERSION: "0.79.2";
@@ -3,4 +3,4 @@
3
3
  /**
4
4
  * Version of this package, used to resolve the prebuilt Shiki engine chunk from a CDN.
5
5
  */
6
- export const UI_VERSION = '0.79.1';
6
+ export const UI_VERSION = '0.79.2';
@@ -0,0 +1,10 @@
1
+ export function escapeBlockSyntax(text: string): string;
2
+ /**
3
+ * Export-only transformer that escapes the Markdown block syntax at the start of a block or of a
4
+ * line after a line break. Without it, a paragraph typed as `## Hours` with the Markdown shortcuts
5
+ * turned off, pasted as text, or imported from the escaped `\## Hours` would be exported as is and
6
+ * become a heading the next time the Markdown is read.
7
+ * @type {TextMatchTransformer}
8
+ */
9
+ export const BLOCK_SYNTAX_ESCAPE: TextMatchTransformer;
10
+ import type { TextMatchTransformer } from '@lexical/markdown';
@@ -0,0 +1,65 @@
1
+ import { $isLineBreakNode as isLineBreakNode, $isTextNode as isTextNode } from 'lexical';
2
+
3
+ /**
4
+ * @import { TextMatchTransformer } from '@lexical/markdown';
5
+ * @import { ElementNode, LexicalNode, TextNode } from 'lexical';
6
+ */
7
+
8
+ /**
9
+ * Escape the Markdown block syntax at the start of a line, so text that only looks like a heading,
10
+ * a list item, a blockquote, a setext heading underline or a thematic break stays text when the
11
+ * Markdown is read again. Lexical already escapes the inline syntax characters, including `*`, `_`,
12
+ * `` ` `` and `~`.
13
+ * @param {string} text Exported Markdown of a line start.
14
+ * @returns {string} Escaped Markdown.
15
+ */
16
+ export const escapeBlockSyntax = (text) =>
17
+ text
18
+ // ATX heading, bullet list item, blockquote, setext heading underline or thematic break
19
+ .replace(/^( {0,3})(#{1,6}(?=[ \t]|$)|[-+](?=[ \t]|$)|>|=+[ \t]*$|-{2,}[ \t]*$)/, '$1\\$2')
20
+ // Ordered list item, whose number can’t be escaped, unlike the delimiter after it
21
+ .replace(/^( {0,3}\d{1,9})([.)])(?=[ \t]|$)/, '$1\\$2');
22
+
23
+ /**
24
+ * Export-only transformer that escapes the Markdown block syntax at the start of a block or of a
25
+ * line after a line break. Without it, a paragraph typed as `## Hours` with the Markdown shortcuts
26
+ * turned off, pasted as text, or imported from the escaped `\## Hours` would be exported as is and
27
+ * become a heading the next time the Markdown is read.
28
+ * @type {TextMatchTransformer}
29
+ */
30
+ export const BLOCK_SYNTAX_ESCAPE = {
31
+ dependencies: [],
32
+ /**
33
+ * Export a text node at the start of a line, escaping the block syntax it starts with.
34
+ * @param {LexicalNode} node Node.
35
+ * @param {(node: ElementNode) => string} _exportChildren Function to export the children of an
36
+ * element, unused.
37
+ * @param {(node: TextNode, textContent: string) => string} exportFormat Function to export a text
38
+ * node with its format.
39
+ * @returns {string | null} Markdown, or `null` to leave the node to the other transformers.
40
+ */
41
+ export: (node, _exportChildren, exportFormat) => {
42
+ if (!isTextNode(node)) {
43
+ return null;
44
+ }
45
+
46
+ const parent = node.getParent();
47
+ const previous = node.getPreviousSibling();
48
+
49
+ // Text in a link or other inline element, in a code block, which is exported verbatim, or in a
50
+ // heading, whose content is only parsed for inline syntax
51
+ if (!parent || parent.isInline() || ['code', 'heading'].includes(parent.getType())) {
52
+ return null;
53
+ }
54
+
55
+ if (previous && !isLineBreakNode(previous)) {
56
+ return null;
57
+ }
58
+
59
+ // A formatted node starts with its own syntax, such as `**`, which is left as is
60
+ return escapeBlockSyntax(exportFormat(node, node.getTextContent()));
61
+ },
62
+ // Never used to import Markdown or as a shortcut
63
+ regExp: /(?!)/,
64
+ type: 'text-match',
65
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sveltia/ui",
3
- "version": "0.79.1",
3
+ "version": "0.79.2",
4
4
  "description": "A collection of Svelte components and utilities for building user interfaces.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -80,7 +80,7 @@
80
80
  "eslint-config-airbnb-extended": "^3.3.0",
81
81
  "eslint-config-prettier": "^10.1.8",
82
82
  "eslint-plugin-import": "^2.32.0",
83
- "eslint-plugin-jsdoc": "^65.0.2",
83
+ "eslint-plugin-jsdoc": "^65.1.0",
84
84
  "eslint-plugin-package-json": "^1.10.1",
85
85
  "eslint-plugin-svelte": "^3.23.0",
86
86
  "globals": "^17.13.0",