marktip 0.7.0__tar.gz → 0.7.1__tar.gz

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.
Files changed (31) hide show
  1. {marktip-0.7.0 → marktip-0.7.1}/CMakeLists.txt +1 -1
  2. {marktip-0.7.0 → marktip-0.7.1}/PKG-INFO +14 -2
  3. {marktip-0.7.0 → marktip-0.7.1}/README.md +13 -1
  4. {marktip-0.7.0 → marktip-0.7.1}/pyproject.toml +1 -1
  5. {marktip-0.7.0 → marktip-0.7.1}/src/ast.cpp +32 -11
  6. {marktip-0.7.0 → marktip-0.7.1}/src/errors.h +2 -1
  7. {marktip-0.7.0 → marktip-0.7.1}/src/marktip.cpp +3 -1
  8. {marktip-0.7.0 → marktip-0.7.1}/src/parser.cpp +29 -9
  9. {marktip-0.7.0 → marktip-0.7.1}/tests/test_parse.py +21 -0
  10. {marktip-0.7.0 → marktip-0.7.1}/tests/test_strict.py +117 -0
  11. {marktip-0.7.0 → marktip-0.7.1}/.github/workflows/build-distributions.yml +0 -0
  12. {marktip-0.7.0 → marktip-0.7.1}/.gitignore +0 -0
  13. {marktip-0.7.0 → marktip-0.7.1}/LICENSE +0 -0
  14. {marktip-0.7.0 → marktip-0.7.1}/python/marktip/__init__.py +0 -0
  15. {marktip-0.7.0 → marktip-0.7.1}/scripts/benchmark.py +0 -0
  16. {marktip-0.7.0 → marktip-0.7.1}/src/ast.h +0 -0
  17. {marktip-0.7.0 → marktip-0.7.1}/src/parser.h +0 -0
  18. {marktip-0.7.0 → marktip-0.7.1}/src/serializer.cpp +0 -0
  19. {marktip-0.7.0 → marktip-0.7.1}/src/serializer.h +0 -0
  20. {marktip-0.7.0 → marktip-0.7.1}/tests/test_document.py +0 -0
  21. {marktip-0.7.0 → marktip-0.7.1}/tests/test_errors.py +0 -0
  22. {marktip-0.7.0 → marktip-0.7.1}/tests/test_roundtrip.py +0 -0
  23. {marktip-0.7.0 → marktip-0.7.1}/tests/test_serialize.py +0 -0
  24. {marktip-0.7.0 → marktip-0.7.1}/tests/test_uri_policy.py +0 -0
  25. {marktip-0.7.0 → marktip-0.7.1}/tests/test_validate.py +0 -0
  26. {marktip-0.7.0 → marktip-0.7.1}/third_party/md4c/LICENSE.md +0 -0
  27. {marktip-0.7.0 → marktip-0.7.1}/third_party/md4c/src/entity.c +0 -0
  28. {marktip-0.7.0 → marktip-0.7.1}/third_party/md4c/src/entity.h +0 -0
  29. {marktip-0.7.0 → marktip-0.7.1}/third_party/md4c/src/md4c.c +0 -0
  30. {marktip-0.7.0 → marktip-0.7.1}/third_party/md4c/src/md4c.h +0 -0
  31. {marktip-0.7.0 → marktip-0.7.1}/third_party/md4c/upstream_metadata/README.md +0 -0
@@ -7,7 +7,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
7
7
  set(CMAKE_CXX_EXTENSIONS OFF)
8
8
 
9
9
  if(NOT DEFINED SKBUILD_PROJECT_VERSION)
10
- set(SKBUILD_PROJECT_VERSION "0.7.0")
10
+ set(SKBUILD_PROJECT_VERSION "0.7.1")
11
11
  endif()
12
12
 
13
13
  set(PYBIND11_FINDPYTHON ON)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: marktip
3
- Version: 0.7.0
3
+ Version: 0.7.1
4
4
  Summary: Fast C++/MD4C Markdown parser and canonical Markdown serializer for Tiptap-style JSON
5
5
  Keywords: markdown,tiptap,md4c,parser
6
6
  Author: Saneaven (Minjae Kyung)
@@ -124,6 +124,13 @@ It is keyword-only and a `from_dict` option only — the parser emits nothing bu
124
124
  | `"content"` | content and structure. Presentation an editor stamped on is type-checked and dropped |
125
125
  | `"exact"` | every attr the JSON carries |
126
126
 
127
+ Both enabled levels also enforce the ProseMirror same-type self-exclusion rule on each node's marks.
128
+ The standard marks represented by marktip are self-exclusive, so a node cannot carry the same mark type twice — including two `link` marks with different destinations.
129
+ The conflicting mark is rejected with `InvalidNodeError` rather than being serialized into Markdown that reparses with different formatting or structure.
130
+ `strict="off"` skips this check because strict validation is disabled.
131
+
132
+ > **Changed in 0.7.1** — `strict="content"` and `strict="exact"` now reject duplicate same-type marks with `invalid_mark_set`. Markdown parsing collapses nested spans that map to the same self-exclusive mark, so parser output remains valid on the strict read path.
133
+
127
134
  The two levels differ over one thing, and it is worth being precise about which:
128
135
 
129
136
  ```python
@@ -231,6 +238,7 @@ For `from_markdown` it locates the node in the parsed document, since the input
231
238
  | `invalid_attr_value` | `InvalidNodeError` | `strict`: the attr's value has the wrong type or is out of range |
232
239
  | `unrepresentable` | `InvalidNodeError` | `strict`: the value is well-formed, but markdown cannot carry it |
233
240
  | `missing_attr` | `InvalidNodeError` | `strict`: a required attr is absent or `None` — a `link` with no `href`, an `image` with no `src` |
241
+ | `invalid_mark_set` | `InvalidNodeError` | `strict`: a node carries conflicting marks of the same type |
234
242
  | `markdown_max_depth` | `ParseError` | markdown nesting exceeds 2048 |
235
243
  | `parse_failed` | `ParseError` | MD4C could not parse the input |
236
244
 
@@ -281,6 +289,9 @@ It deliberately does **not** validate, unless `strict` asks it to:
281
289
  A `heading` without `level` or a `codeBlock` without `language` is accepted; the serializer substitutes a default rather than failing.
282
290
  `strict` adds exactly two: a `link` needs an `href` and an `image` needs a `src`, because the default there is an empty destination that reads back as a real link.
283
291
  `{"src": None}` and no `src` key at all are the same thing to every part of the library, the URI policy included.
292
+ - **Mark sets.**
293
+ With strict validation disabled, two marks of the same type may appear on one node and are serialized independently.
294
+ `strict="content"` and `strict="exact"` reject the second mark because marktip's standard ProseMirror/Tiptap marks are self-exclusive.
284
295
  - **Content models.**
285
296
  Which node may contain which is not checked — a `text` node directly under `doc` is accepted and serialized.
286
297
  - **Raw HTML.**
@@ -305,7 +316,7 @@ except tm.InvalidNodeError as e:
305
316
  ## Defined normalizations
306
317
 
307
318
  Markdown cannot represent every Tiptap document exactly.
308
- Rather than emitting markdown that reparses into a different structure, marktip applies these deterministic (and idempotent) normalizations at serialization time:
319
+ Rather than emitting or retaining a representation that changes on the next conversion, marktip applies these deterministic (and idempotent) normalizations during parsing and serialization:
309
320
 
310
321
  - **Hard breaks in headings** — ATX headings are single-line, so a `hardBreak` (or a literal newline carried over from setext input) inside a heading serializes as a single space: `heading("a", hardBreak, "b")` → `# a b`.
311
322
  - **Multi-block table cells** — blocks inside a cell are joined with `<br>` and the cell is flattened to one line; two paragraphs in a cell reparse as one paragraph containing a `hardBreak`.
@@ -313,6 +324,7 @@ Rather than emitting markdown that reparses into a different structure, marktip
313
324
  - **Heading levels** — clamped to 1–6 at serialization (`0` → `#`, `7` → `######`).
314
325
  - **Ordered list start** — clamped to CommonMark's 0–999999999 (`-5` → `0.`), and the running number stops at that ceiling rather than emitting a 10-digit marker. Only the first number is honoured on reparse, so a repeated ceiling changes nothing.
315
326
  - **Code fence info strings** — a `language` is truncated at the first newline, since an info string is a single line; one containing a backtick switches the fence to `~~~`, which carries it losslessly.
327
+ - **Nested same-type marks** — Markdown spans that map to the same self-exclusive mark are collapsed (`*outer _inner_ outer*` becomes one continuous `italic` mark) rather than producing an invalid ProseMirror mark set.
316
328
  - **Emphasis boundary whitespace** — whitespace touching an emphasis delimiter has no valid markdown form and is expelled outside the marks (`bold("굵게 ")` → `**굵게** `), cf. prosemirror-markdown's `expelEnclosingWhitespace`.
317
329
  - **Adjacent same-family lists** — consecutive lists of the same family alternate markers (`-`/`*`, `1.`/`1)`) so they stay separate lists on reparse instead of merging (which would renumber ordered items or spread task checkboxes).
318
330
 
@@ -96,6 +96,13 @@ It is keyword-only and a `from_dict` option only — the parser emits nothing bu
96
96
  | `"content"` | content and structure. Presentation an editor stamped on is type-checked and dropped |
97
97
  | `"exact"` | every attr the JSON carries |
98
98
 
99
+ Both enabled levels also enforce the ProseMirror same-type self-exclusion rule on each node's marks.
100
+ The standard marks represented by marktip are self-exclusive, so a node cannot carry the same mark type twice — including two `link` marks with different destinations.
101
+ The conflicting mark is rejected with `InvalidNodeError` rather than being serialized into Markdown that reparses with different formatting or structure.
102
+ `strict="off"` skips this check because strict validation is disabled.
103
+
104
+ > **Changed in 0.7.1** — `strict="content"` and `strict="exact"` now reject duplicate same-type marks with `invalid_mark_set`. Markdown parsing collapses nested spans that map to the same self-exclusive mark, so parser output remains valid on the strict read path.
105
+
99
106
  The two levels differ over one thing, and it is worth being precise about which:
100
107
 
101
108
  ```python
@@ -203,6 +210,7 @@ For `from_markdown` it locates the node in the parsed document, since the input
203
210
  | `invalid_attr_value` | `InvalidNodeError` | `strict`: the attr's value has the wrong type or is out of range |
204
211
  | `unrepresentable` | `InvalidNodeError` | `strict`: the value is well-formed, but markdown cannot carry it |
205
212
  | `missing_attr` | `InvalidNodeError` | `strict`: a required attr is absent or `None` — a `link` with no `href`, an `image` with no `src` |
213
+ | `invalid_mark_set` | `InvalidNodeError` | `strict`: a node carries conflicting marks of the same type |
206
214
  | `markdown_max_depth` | `ParseError` | markdown nesting exceeds 2048 |
207
215
  | `parse_failed` | `ParseError` | MD4C could not parse the input |
208
216
 
@@ -253,6 +261,9 @@ It deliberately does **not** validate, unless `strict` asks it to:
253
261
  A `heading` without `level` or a `codeBlock` without `language` is accepted; the serializer substitutes a default rather than failing.
254
262
  `strict` adds exactly two: a `link` needs an `href` and an `image` needs a `src`, because the default there is an empty destination that reads back as a real link.
255
263
  `{"src": None}` and no `src` key at all are the same thing to every part of the library, the URI policy included.
264
+ - **Mark sets.**
265
+ With strict validation disabled, two marks of the same type may appear on one node and are serialized independently.
266
+ `strict="content"` and `strict="exact"` reject the second mark because marktip's standard ProseMirror/Tiptap marks are self-exclusive.
256
267
  - **Content models.**
257
268
  Which node may contain which is not checked — a `text` node directly under `doc` is accepted and serialized.
258
269
  - **Raw HTML.**
@@ -277,7 +288,7 @@ except tm.InvalidNodeError as e:
277
288
  ## Defined normalizations
278
289
 
279
290
  Markdown cannot represent every Tiptap document exactly.
280
- Rather than emitting markdown that reparses into a different structure, marktip applies these deterministic (and idempotent) normalizations at serialization time:
291
+ Rather than emitting or retaining a representation that changes on the next conversion, marktip applies these deterministic (and idempotent) normalizations during parsing and serialization:
281
292
 
282
293
  - **Hard breaks in headings** — ATX headings are single-line, so a `hardBreak` (or a literal newline carried over from setext input) inside a heading serializes as a single space: `heading("a", hardBreak, "b")` → `# a b`.
283
294
  - **Multi-block table cells** — blocks inside a cell are joined with `<br>` and the cell is flattened to one line; two paragraphs in a cell reparse as one paragraph containing a `hardBreak`.
@@ -285,6 +296,7 @@ Rather than emitting markdown that reparses into a different structure, marktip
285
296
  - **Heading levels** — clamped to 1–6 at serialization (`0` → `#`, `7` → `######`).
286
297
  - **Ordered list start** — clamped to CommonMark's 0–999999999 (`-5` → `0.`), and the running number stops at that ceiling rather than emitting a 10-digit marker. Only the first number is honoured on reparse, so a repeated ceiling changes nothing.
287
298
  - **Code fence info strings** — a `language` is truncated at the first newline, since an info string is a single line; one containing a backtick switches the fence to `~~~`, which carries it losslessly.
299
+ - **Nested same-type marks** — Markdown spans that map to the same self-exclusive mark are collapsed (`*outer _inner_ outer*` becomes one continuous `italic` mark) rather than producing an invalid ProseMirror mark set.
288
300
  - **Emphasis boundary whitespace** — whitespace touching an emphasis delimiter has no valid markdown form and is expelled outside the marks (`bold("굵게 ")` → `**굵게** `), cf. prosemirror-markdown's `expelEnclosingWhitespace`.
289
301
  - **Adjacent same-family lists** — consecutive lists of the same family alternate markers (`-`/`*`, `1.`/`1)`) so they stay separate lists on reparse instead of merging (which would renumber ordered items or spread task checkboxes).
290
302
 
@@ -4,7 +4,7 @@ build-backend = "scikit_build_core.build"
4
4
 
5
5
  [project]
6
6
  name = "marktip"
7
- version = "0.7.0"
7
+ version = "0.7.1"
8
8
  description = "Fast C++/MD4C Markdown parser and canonical Markdown serializer for Tiptap-style JSON"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.9"
@@ -555,6 +555,37 @@ Mark mark_from_py(py::dict mark_dict, StrictMode mode, const PathStack& path) {
555
555
  return mark;
556
556
  }
557
557
 
558
+ // Keep mark parsing outside fill_node_from_py's recursive frame: kMaxNodeDepth must
559
+ // remain safe on the smaller Windows worker stack even as strict checks grow.
560
+ void fill_marks_from_py(Node& node, py::object marks, StrictMode mode, PathStack& path) {
561
+ if (marks.is_none()) {
562
+ return;
563
+ }
564
+
565
+ py::list mark_list = expect_list(marks, "node marks", "marks", path);
566
+ std::size_t mark_index = 0;
567
+ for (py::handle mark_handle : mark_list) {
568
+ path.push_back({"marks", mark_index});
569
+ Mark mark = mark_from_py(expect_dict(mark_handle, "mark", "marks", path), mode, path);
570
+ if (mode != StrictMode::Off) {
571
+ // Marktip's standard ProseMirror marks are self-exclusive. This is a strict
572
+ // semantic check: Off intentionally stores the collection exactly as supplied.
573
+ bool conflicts = std::any_of(node.marks.begin(), node.marks.end(), [&](const Mark& existing) {
574
+ return existing.type == mark.type;
575
+ });
576
+ if (conflicts) {
577
+ throw InvalidNodeError("invalid_mark_set", "marks",
578
+ "mark type '" + mark.type +
579
+ "' conflicts with an earlier mark of the same type",
580
+ format_path(path));
581
+ }
582
+ }
583
+ node.marks.push_back(std::move(mark));
584
+ path.pop_back();
585
+ ++mark_index;
586
+ }
587
+ }
588
+
558
589
  void fill_node_from_py(Document& document, std::size_t index, py::dict node_dict, std::string_view context,
559
590
  std::size_t depth, StrictMode mode, PathStack& path) {
560
591
  if (depth > kMaxNodeDepth) {
@@ -587,17 +618,7 @@ void fill_node_from_py(Document& document, std::size_t index, py::dict node_dict
587
618
  check_required_attrs(node.attrs, table, node.type, "node", path);
588
619
  }
589
620
 
590
- py::object marks = dict_get(node_dict, "marks");
591
- if (!marks.is_none()) {
592
- py::list mark_list = expect_list(marks, "node marks", "marks", path);
593
- std::size_t mark_index = 0;
594
- for (py::handle mark_handle : mark_list) {
595
- path.push_back({"marks", mark_index});
596
- node.marks.push_back(mark_from_py(expect_dict(mark_handle, "mark", "marks", path), mode, path));
597
- path.pop_back();
598
- ++mark_index;
599
- }
600
- }
621
+ fill_marks_from_py(node, dict_get(node_dict, "marks"), mode, path);
601
622
 
602
623
  document.node(index) = std::move(node);
603
624
 
@@ -59,7 +59,8 @@ public:
59
59
  const std::string& field() const noexcept { return field_; }
60
60
 
61
61
  private:
62
- // "type" | "attrs" | "marks" | "content", or the offending attr's own name
62
+ // "type" | "attrs" | "marks" | "content", or the offending attr's own name.
63
+ // "marks" also names a strict mark-set conflict located at the offending mark.
63
64
  // ("href" | "src" for the URI policy, any attr key under strict).
64
65
  std::string field_;
65
66
  };
@@ -11,7 +11,7 @@
11
11
  namespace py = pybind11;
12
12
 
13
13
  #ifndef MARKTIP_VERSION
14
- #define MARKTIP_VERSION "0.7.0"
14
+ #define MARKTIP_VERSION "0.7.1"
15
15
  #endif
16
16
 
17
17
  namespace {
@@ -140,6 +140,8 @@ PYBIND11_MODULE(_core, module) {
140
140
  " 'unknown_attr' (a name outside both dialects), 'invalid_attr_value' (wrong type or out of range),"
141
141
  " 'unrepresentable' (well-formed, but GFM cannot carry it, e.g. colspan=2)"
142
142
  " or 'missing_attr' (a link with no href, an image with no src, which would serialize as '[a]()')."
143
+ " With strict enabled, a duplicate same-type mark raises InvalidNodeError"
144
+ " with .field 'marks' and .code 'invalid_mark_set'."
143
145
  " table 'colCount' and a non-header row's 'align' are accepted at every level,"
144
146
  " because from_markdown emits both and the structure carries them anyway."
145
147
  " None means the attr was never set wherever Tiptap declares `default: null`,"
@@ -8,7 +8,6 @@ extern "C" {
8
8
 
9
9
  #include <algorithm>
10
10
  #include <cstddef>
11
- #include <iterator>
12
11
  #include <string>
13
12
  #include <string_view>
14
13
  #include <utility>
@@ -279,10 +278,10 @@ public:
279
278
  int enter_span(MD_SPANTYPE type, void* detail) {
280
279
  switch (type) {
281
280
  case MD_SPAN_EM:
282
- active_marks_.push_back(Mark{"italic", {}});
281
+ push_mark(Mark{"italic", {}});
283
282
  return 0;
284
283
  case MD_SPAN_STRONG:
285
- active_marks_.push_back(Mark{"bold", {}});
284
+ push_mark(Mark{"bold", {}});
286
285
  return 0;
287
286
  case MD_SPAN_A: {
288
287
  auto* link = static_cast<MD_SPAN_A_DETAIL*>(detail);
@@ -294,7 +293,7 @@ public:
294
293
  set_attr(attrs, "title", AttrValue::string(std::move(title)));
295
294
  }
296
295
  }
297
- active_marks_.push_back(Mark{"link", std::move(attrs)});
296
+ push_mark(Mark{"link", std::move(attrs)});
298
297
  return 0;
299
298
  }
300
299
  case MD_SPAN_IMG: {
@@ -314,10 +313,10 @@ public:
314
313
  return 0;
315
314
  }
316
315
  case MD_SPAN_CODE:
317
- active_marks_.push_back(Mark{"code", {}});
316
+ push_mark(Mark{"code", {}});
318
317
  return 0;
319
318
  case MD_SPAN_DEL:
320
- active_marks_.push_back(Mark{"strike", {}});
319
+ push_mark(Mark{"strike", {}});
321
320
  return 0;
322
321
  case MD_SPAN_LATEXMATH:
323
322
  case MD_SPAN_LATEXMATH_DISPLAY:
@@ -419,6 +418,7 @@ private:
419
418
  Document document_;
420
419
  std::vector<std::size_t> stack_;
421
420
  std::vector<Mark> active_marks_;
421
+ std::vector<std::size_t> active_mark_depths_;
422
422
  std::string error_;
423
423
  std::string error_code_;
424
424
  bool html_ = true;
@@ -455,10 +455,30 @@ private:
455
455
  }
456
456
  }
457
457
 
458
+ void push_mark(Mark mark) {
459
+ // MD4C can report nested spans that map to the same ProseMirror mark,
460
+ // such as *outer _inner_ outer*. Keep their callback depth, but expose
461
+ // one self-exclusive mark in the AST produced for nodes inside them.
462
+ for (std::size_t i = 0; i < active_marks_.size(); ++i) {
463
+ if (active_marks_[i].type == mark.type) {
464
+ ++active_mark_depths_[i];
465
+ return;
466
+ }
467
+ }
468
+ active_marks_.push_back(std::move(mark));
469
+ active_mark_depths_.push_back(1);
470
+ }
471
+
458
472
  void pop_mark(std::string_view type) {
459
- for (auto it = active_marks_.rbegin(); it != active_marks_.rend(); ++it) {
460
- if (it->type == type) {
461
- active_marks_.erase(std::next(it).base());
473
+ for (std::size_t i = active_marks_.size(); i > 0; --i) {
474
+ std::size_t index = i - 1;
475
+ if (active_marks_[index].type == type) {
476
+ if (active_mark_depths_[index] > 1) {
477
+ --active_mark_depths_[index];
478
+ return;
479
+ }
480
+ active_marks_.erase(active_marks_.begin() + index);
481
+ active_mark_depths_.erase(active_mark_depths_.begin() + index);
462
482
  return;
463
483
  }
464
484
  }
@@ -28,6 +28,27 @@ def test_parse_heading_marks_and_link():
28
28
  }
29
29
 
30
30
 
31
+ def test_parse_collapses_nested_same_type_spans_into_one_self_exclusive_mark():
32
+ cases = [
33
+ ("****x****", "x", "bold"),
34
+ ("*outer _inner_ outer*", "outer inner outer", "italic"),
35
+ ("**outer __inner__ outer**", "outer inner outer", "bold"),
36
+ ("~~outer ~~inner~~ outer~~", "outer inner outer", "strike"),
37
+ ]
38
+
39
+ for markdown, expected_text, mark_type in cases:
40
+ content = tm.from_markdown(markdown).to_dict()["content"][0]["content"]
41
+ assert content == [
42
+ {"type": "text", "text": expected_text, "marks": [{"type": mark_type}]},
43
+ ]
44
+
45
+ content = tm.from_markdown("*outer _inner_ outer* plain").to_dict()["content"][0]["content"]
46
+ assert content == [
47
+ {"type": "text", "text": "outer inner outer", "marks": [{"type": "italic"}]},
48
+ {"type": "text", "text": " plain"},
49
+ ]
50
+
51
+
31
52
  def test_parse_gfm_task_list_and_table():
32
53
  ast = tm.from_markdown("- [x] done\n- [ ] todo\n\n| A | B |\n| :- | -: |\n| x | y |\n").to_dict()
33
54
 
@@ -16,6 +16,10 @@ onto every node — so the caller says which loss is acceptable: "content" or "e
16
16
  It is from_dict-only, unlike the URI policy: the parser only ever emits attrs it
17
17
  understands, with values in range, so there is nothing on the from_markdown path for it
18
18
  to catch. test_strict_accepts_what_the_parser_produces is the guard on that claim.
19
+
20
+ Issue #8 adds the collection-level half of that promise: when strict validation is
21
+ enabled, self-exclusive ProseMirror marks cannot repeat on a node. With strict="off",
22
+ that validation is intentionally not performed.
19
23
  """
20
24
 
21
25
  import pytest
@@ -35,6 +39,17 @@ def text(value="x"):
35
39
  return {"type": "text", "text": value}
36
40
 
37
41
 
42
+ def marked(*marks):
43
+ return doc(p({"type": "text", "text": "x", "marks": list(marks)}))
44
+
45
+
46
+ def mark(type_, **attrs):
47
+ value = {"type": type_}
48
+ if attrs:
49
+ value["attrs"] = attrs
50
+ return value
51
+
52
+
38
53
  def node(type_, **attrs):
39
54
  return {"type": type_, "attrs": attrs, "content": [p(text())]}
40
55
 
@@ -67,6 +82,7 @@ def code_block(**attrs):
67
82
  CELL_PATH = "content[0].content[0].content[0]"
68
83
  LINK_PATH = "content[0].content[0].marks[0]"
69
84
  IMAGE_PATH = "content[0].content[0]"
85
+ SECOND_MARK_PATH = "content[0].content[0].marks[1]"
70
86
 
71
87
  # Both levels checked everywhere the two are meant to agree, which is everywhere but
72
88
  # the presentation attrs below. A rule that only ever ran at one level would be a rule
@@ -112,6 +128,104 @@ def test_strict_is_off_by_default():
112
128
  assert code_for(cell(colspan=2), strict="content") == "unrepresentable"
113
129
 
114
130
 
131
+ # ── invalid_mark_set: self-exclusive marks cannot repeat ────────────────────
132
+
133
+
134
+ DUPLICATE_MARK_CASES = [
135
+ pytest.param([mark("bold"), mark("bold")], id="bold"),
136
+ pytest.param([mark("italic"), mark("italic")], id="italic"),
137
+ pytest.param([mark("strike"), mark("strike")], id="strike"),
138
+ pytest.param([mark("code"), mark("code")], id="code"),
139
+ pytest.param(
140
+ [mark("link", href="https://a.example"), mark("link", href="https://a.example")],
141
+ id="identical-link",
142
+ ),
143
+ pytest.param(
144
+ [mark("link", href="https://a.example"), mark("link", href="https://b.example")],
145
+ id="conflicting-link",
146
+ ),
147
+ ]
148
+
149
+
150
+ @pytest.mark.parametrize("level", LEVELS)
151
+ @pytest.mark.parametrize("marks", DUPLICATE_MARK_CASES)
152
+ def test_strict_rejects_duplicate_same_type_marks(level, marks):
153
+ with pytest.raises(tm.InvalidNodeError) as excinfo:
154
+ tm.from_dict(marked(*marks), strict=level)
155
+
156
+ err = excinfo.value
157
+ assert err.code == "invalid_mark_set"
158
+ assert err.field == "marks"
159
+ assert err.path == SECOND_MARK_PATH
160
+ assert str(err) == f"mark type '{marks[1]['type']}' conflicts with an earlier mark of the same type"
161
+ assert err.detail == str(err)
162
+
163
+
164
+ @pytest.mark.parametrize("marks", DUPLICATE_MARK_CASES)
165
+ def test_mark_set_validation_is_skipped_when_strict_is_off(marks):
166
+ # Off means the strict mark-set check is disabled; accepting these inputs is
167
+ # the option's semantics, not a compatibility exception.
168
+ ast = marked(*marks)
169
+ document = tm.from_dict(ast, strict="off")
170
+ assert document.to_dict() == ast
171
+ assert isinstance(document.to_markdown(), str)
172
+
173
+
174
+ @pytest.mark.parametrize("level", LEVELS)
175
+ def test_strict_reports_a_nonadjacent_duplicate_at_the_conflicting_mark(level):
176
+ ast = marked(mark("italic"), mark("bold"), mark("italic"))
177
+
178
+ with pytest.raises(tm.InvalidNodeError) as excinfo:
179
+ tm.from_dict(ast, strict=level)
180
+
181
+ assert excinfo.value.code == "invalid_mark_set"
182
+ assert excinfo.value.path == "content[0].content[0].marks[2]"
183
+
184
+
185
+ @pytest.mark.parametrize("level", LEVELS)
186
+ def test_strict_checks_mark_sets_on_non_text_inline_nodes(level):
187
+ ast = doc(
188
+ p(
189
+ {
190
+ "type": "image",
191
+ "attrs": {"src": "x.png"},
192
+ "marks": [mark("link", href="https://a.example"), mark("link", href="https://b.example")],
193
+ }
194
+ )
195
+ )
196
+
197
+ with pytest.raises(tm.InvalidNodeError) as excinfo:
198
+ tm.from_dict(ast, strict=level)
199
+
200
+ assert excinfo.value.code == "invalid_mark_set"
201
+ assert excinfo.value.path == SECOND_MARK_PATH
202
+
203
+
204
+ @pytest.mark.parametrize("level", LEVELS)
205
+ def test_strict_allows_distinct_mark_types_and_reuse_on_another_node(level):
206
+ ast = doc(
207
+ p(
208
+ {"type": "text", "text": "a", "marks": [mark("italic"), mark("bold")]},
209
+ {"type": "text", "text": "b", "marks": [mark("bold"), mark("italic")]},
210
+ {"type": "text", "text": "c", "marks": [mark("italic"), mark("link", href="https://a.example")]},
211
+ )
212
+ )
213
+
214
+ assert code_for(ast, strict=level) is None
215
+
216
+
217
+ @pytest.mark.parametrize("level", LEVELS)
218
+ def test_an_invalid_duplicate_mark_is_reported_before_the_set_conflict(level):
219
+ ast = marked(mark("link", href="https://a.example"), mark("link"))
220
+
221
+ with pytest.raises(tm.InvalidNodeError) as excinfo:
222
+ tm.from_dict(ast, strict=level)
223
+
224
+ assert excinfo.value.code == "missing_attr"
225
+ assert excinfo.value.field == "href"
226
+ assert excinfo.value.path == SECOND_MARK_PATH
227
+
228
+
115
229
  # ── unknown_attr: a name outside both dialects ───────────────────────────────
116
230
 
117
231
 
@@ -575,6 +689,9 @@ def test_col_count_is_still_an_int(level):
575
689
 
576
690
  PARSER_CORPUS = [
577
691
  "# h1\n\n## h2\n\ntext with **bold**, *em*, `code` and ~~strike~~\n",
692
+ "*outer _inner_ outer*\n",
693
+ "**outer __inner__ outer**\n",
694
+ "~~outer ~~inner~~ outer~~\n",
578
695
  "| a | b | c |\n| :-- | :-: | --: |\n| 1 | 2 | 3 |\n| 4 | 5 | 6 |\n",
579
696
  "- a\n- b\n - nested\n\n1. one\n2. two\n\n5. five\n",
580
697
  "- [ ] todo\n- [x] done\n",
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes