marklassian 0.1.0__py3-none-any.whl
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.
- marklassian/__init__.py +7 -0
- marklassian/converter.py +473 -0
- marklassian/types.py +20 -0
- marklassian-0.1.0.dist-info/METADATA +123 -0
- marklassian-0.1.0.dist-info/RECORD +7 -0
- marklassian-0.1.0.dist-info/WHEEL +4 -0
- marklassian-0.1.0.dist-info/licenses/LICENSE +21 -0
marklassian/__init__.py
ADDED
marklassian/converter.py
ADDED
|
@@ -0,0 +1,473 @@
|
|
|
1
|
+
import re
|
|
2
|
+
import uuid
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
import mistune
|
|
6
|
+
|
|
7
|
+
from .types import AdfDocument, AdfMark, AdfNode
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _generate_local_id() -> str:
|
|
11
|
+
return str(uuid.uuid4())
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _get_safe_text(token: dict[str, Any]) -> str:
|
|
15
|
+
children = token.get("children")
|
|
16
|
+
if children and len(children) == 1:
|
|
17
|
+
return _get_safe_text(children[0])
|
|
18
|
+
|
|
19
|
+
if children:
|
|
20
|
+
texts = [_get_safe_text(child) for child in children]
|
|
21
|
+
combined = "".join(texts)
|
|
22
|
+
return re.sub(r"\s+", " ", combined)
|
|
23
|
+
|
|
24
|
+
raw = token.get("raw", "")
|
|
25
|
+
if isinstance(raw, str):
|
|
26
|
+
text = raw.rstrip("\n")
|
|
27
|
+
text = text.replace("\n", " ")
|
|
28
|
+
text = re.sub(r"\s+", " ", text)
|
|
29
|
+
return text
|
|
30
|
+
return ""
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _get_marks(token: dict[str, Any], marks: dict[str, AdfMark] | None = None) -> list[AdfMark]:
|
|
34
|
+
if marks is None:
|
|
35
|
+
marks = {}
|
|
36
|
+
|
|
37
|
+
token_type = token.get("type", "")
|
|
38
|
+
|
|
39
|
+
if token_type == "emphasis" and "em" not in marks:
|
|
40
|
+
marks["em"] = {"type": "em"}
|
|
41
|
+
|
|
42
|
+
if token_type == "strong" and "strong" not in marks:
|
|
43
|
+
marks["strong"] = {"type": "strong"}
|
|
44
|
+
|
|
45
|
+
if token_type == "strikethrough" and "strike" not in marks:
|
|
46
|
+
marks["strike"] = {"type": "strike"}
|
|
47
|
+
|
|
48
|
+
if token_type == "link":
|
|
49
|
+
marks["link"] = {"type": "link", "attrs": {"href": token.get("attrs", {}).get("url", "")}}
|
|
50
|
+
|
|
51
|
+
if token_type == "codespan" and "code" not in marks:
|
|
52
|
+
marks["code"] = {"type": "code"}
|
|
53
|
+
|
|
54
|
+
children = token.get("children", [])
|
|
55
|
+
if children and len(children) == 1:
|
|
56
|
+
return _get_marks(children[0], marks)
|
|
57
|
+
|
|
58
|
+
resolved_marks = list(marks.values())
|
|
59
|
+
|
|
60
|
+
if "code" in marks:
|
|
61
|
+
return [m for m in resolved_marks if m["type"] in ("link", "code")]
|
|
62
|
+
|
|
63
|
+
return resolved_marks
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _create_media_node(token: dict[str, Any]) -> AdfNode:
|
|
67
|
+
attrs = token.get("attrs", {})
|
|
68
|
+
children = token.get("children", [])
|
|
69
|
+
alt_text = ""
|
|
70
|
+
if children and children[0].get("type") == "text":
|
|
71
|
+
alt_text = children[0].get("raw", "")
|
|
72
|
+
return {
|
|
73
|
+
"type": "mediaSingle",
|
|
74
|
+
"attrs": {"layout": "center"},
|
|
75
|
+
"content": [
|
|
76
|
+
{
|
|
77
|
+
"type": "media",
|
|
78
|
+
"attrs": {
|
|
79
|
+
"type": "external",
|
|
80
|
+
"url": attrs.get("url", ""),
|
|
81
|
+
"alt": alt_text,
|
|
82
|
+
},
|
|
83
|
+
}
|
|
84
|
+
],
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def _merge_adjacent_text_nodes(nodes: list[AdfNode]) -> list[AdfNode]:
|
|
89
|
+
if not nodes:
|
|
90
|
+
return []
|
|
91
|
+
|
|
92
|
+
result: list[AdfNode] = []
|
|
93
|
+
for node in nodes:
|
|
94
|
+
if node.get("type") != "text":
|
|
95
|
+
result.append(node)
|
|
96
|
+
continue
|
|
97
|
+
|
|
98
|
+
if not result:
|
|
99
|
+
result.append(node)
|
|
100
|
+
continue
|
|
101
|
+
|
|
102
|
+
prev = result[-1]
|
|
103
|
+
if prev.get("type") != "text":
|
|
104
|
+
result.append(node)
|
|
105
|
+
continue
|
|
106
|
+
|
|
107
|
+
prev_marks = prev.get("marks", [])
|
|
108
|
+
curr_marks = node.get("marks", [])
|
|
109
|
+
if prev_marks != curr_marks:
|
|
110
|
+
result.append(node)
|
|
111
|
+
continue
|
|
112
|
+
|
|
113
|
+
prev["text"] = prev.get("text", "") + node.get("text", "")
|
|
114
|
+
|
|
115
|
+
return result
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def _inline_to_adf(tokens: list[dict[str, Any]] | None) -> list[AdfNode]:
|
|
119
|
+
if not tokens:
|
|
120
|
+
return []
|
|
121
|
+
|
|
122
|
+
result: list[AdfNode] = []
|
|
123
|
+
|
|
124
|
+
for token in tokens:
|
|
125
|
+
token_type = token.get("type", "")
|
|
126
|
+
|
|
127
|
+
if token_type == "text":
|
|
128
|
+
children = token.get("children")
|
|
129
|
+
if children:
|
|
130
|
+
result.extend(_inline_to_adf(children))
|
|
131
|
+
else:
|
|
132
|
+
text = _get_safe_text(token)
|
|
133
|
+
if text:
|
|
134
|
+
result.append({"type": "text", "text": text})
|
|
135
|
+
|
|
136
|
+
elif token_type == "emphasis":
|
|
137
|
+
children = token.get("children", [])
|
|
138
|
+
for child in children:
|
|
139
|
+
text = _get_safe_text(child)
|
|
140
|
+
if text:
|
|
141
|
+
result.append({
|
|
142
|
+
"type": "text",
|
|
143
|
+
"text": text,
|
|
144
|
+
"marks": _get_marks(child, {"em": {"type": "em"}}),
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
elif token_type == "strong":
|
|
148
|
+
children = token.get("children", [])
|
|
149
|
+
for child in children:
|
|
150
|
+
text = _get_safe_text(child)
|
|
151
|
+
if text:
|
|
152
|
+
result.append({
|
|
153
|
+
"type": "text",
|
|
154
|
+
"text": text,
|
|
155
|
+
"marks": _get_marks(child, {"strong": {"type": "strong"}}),
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
elif token_type == "strikethrough":
|
|
159
|
+
children = token.get("children", [])
|
|
160
|
+
for child in children:
|
|
161
|
+
text = _get_safe_text(child)
|
|
162
|
+
if text:
|
|
163
|
+
result.append({
|
|
164
|
+
"type": "text",
|
|
165
|
+
"text": text,
|
|
166
|
+
"marks": _get_marks(child, {"strike": {"type": "strike"}}),
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
elif token_type == "link":
|
|
170
|
+
text = _get_safe_text(token)
|
|
171
|
+
if text:
|
|
172
|
+
result.append({
|
|
173
|
+
"type": "text",
|
|
174
|
+
"text": text,
|
|
175
|
+
"marks": _get_marks(token),
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
elif token_type == "codespan":
|
|
179
|
+
text = _get_safe_text(token)
|
|
180
|
+
if text:
|
|
181
|
+
result.append({
|
|
182
|
+
"type": "text",
|
|
183
|
+
"text": text,
|
|
184
|
+
"marks": _get_marks(token),
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
elif token_type == "linebreak":
|
|
188
|
+
result.append({"type": "hardBreak"})
|
|
189
|
+
|
|
190
|
+
elif token_type == "softbreak":
|
|
191
|
+
result.append({"type": "text", "text": " "})
|
|
192
|
+
|
|
193
|
+
elif token_type == "block_text":
|
|
194
|
+
result.extend(_inline_to_adf(token.get("children", [])))
|
|
195
|
+
|
|
196
|
+
filtered = [node for node in result if not (node.get("type") == "text" and not node.get("text"))]
|
|
197
|
+
return _merge_adjacent_text_nodes(filtered)
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def _strip_trailing_softbreaks(tokens: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
|
201
|
+
while tokens and tokens[-1].get("type") == "softbreak":
|
|
202
|
+
tokens = tokens[:-1]
|
|
203
|
+
return tokens
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def _process_paragraph(tokens: list[dict[str, Any]] | None) -> list[AdfNode]:
|
|
207
|
+
if not tokens:
|
|
208
|
+
return []
|
|
209
|
+
|
|
210
|
+
if len(tokens) == 1 and tokens[0].get("type") == "image":
|
|
211
|
+
return [_create_media_node(tokens[0])]
|
|
212
|
+
|
|
213
|
+
output_nodes: list[AdfNode] = []
|
|
214
|
+
current_paragraph_tokens: list[dict[str, Any]] = []
|
|
215
|
+
|
|
216
|
+
for token in tokens:
|
|
217
|
+
if token.get("type") == "image":
|
|
218
|
+
if current_paragraph_tokens:
|
|
219
|
+
trimmed = _strip_trailing_softbreaks(current_paragraph_tokens)
|
|
220
|
+
if trimmed:
|
|
221
|
+
output_nodes.append({
|
|
222
|
+
"type": "paragraph",
|
|
223
|
+
"content": _inline_to_adf(trimmed),
|
|
224
|
+
})
|
|
225
|
+
current_paragraph_tokens = []
|
|
226
|
+
output_nodes.append(_create_media_node(token))
|
|
227
|
+
else:
|
|
228
|
+
current_paragraph_tokens.append(token)
|
|
229
|
+
|
|
230
|
+
if current_paragraph_tokens:
|
|
231
|
+
output_nodes.append({
|
|
232
|
+
"type": "paragraph",
|
|
233
|
+
"content": _inline_to_adf(current_paragraph_tokens),
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
return output_nodes
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def _is_task_list(items: list[dict[str, Any]]) -> bool:
|
|
240
|
+
if not items:
|
|
241
|
+
return False
|
|
242
|
+
return all(item.get("type") == "task_list_item" for item in items)
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def _process_task_item(item: dict[str, Any]) -> AdfNode:
|
|
246
|
+
item_content: list[AdfNode] = []
|
|
247
|
+
current_paragraph_tokens: list[dict[str, Any]] = []
|
|
248
|
+
|
|
249
|
+
inline_types = {"text", "emphasis", "strong", "strikethrough", "link", "codespan", "block_text"}
|
|
250
|
+
|
|
251
|
+
for token in item.get("children", []):
|
|
252
|
+
token_type = token.get("type", "")
|
|
253
|
+
|
|
254
|
+
if token_type in inline_types:
|
|
255
|
+
current_paragraph_tokens.append(token)
|
|
256
|
+
else:
|
|
257
|
+
if current_paragraph_tokens:
|
|
258
|
+
item_content.extend(_inline_to_adf(current_paragraph_tokens))
|
|
259
|
+
current_paragraph_tokens = []
|
|
260
|
+
|
|
261
|
+
if token_type == "list":
|
|
262
|
+
list_items = token.get("children", [])
|
|
263
|
+
if _is_task_list(list_items):
|
|
264
|
+
item_content.append({
|
|
265
|
+
"type": "taskList",
|
|
266
|
+
"attrs": {"localId": _generate_local_id()},
|
|
267
|
+
"content": [_process_task_item(li) for li in list_items],
|
|
268
|
+
})
|
|
269
|
+
else:
|
|
270
|
+
is_ordered = token.get("attrs", {}).get("ordered", False)
|
|
271
|
+
start = token.get("attrs", {}).get("start", 1)
|
|
272
|
+
list_node: AdfNode = {
|
|
273
|
+
"type": "orderedList" if is_ordered else "bulletList",
|
|
274
|
+
"content": [_process_list_item(li) for li in list_items],
|
|
275
|
+
}
|
|
276
|
+
if is_ordered:
|
|
277
|
+
list_node["attrs"] = {"order": start}
|
|
278
|
+
item_content.append(list_node)
|
|
279
|
+
else:
|
|
280
|
+
processed = _tokens_to_adf([token])
|
|
281
|
+
item_content.extend(processed)
|
|
282
|
+
|
|
283
|
+
if current_paragraph_tokens:
|
|
284
|
+
item_content.extend(_inline_to_adf(current_paragraph_tokens))
|
|
285
|
+
|
|
286
|
+
checked = item.get("attrs", {}).get("checked", False)
|
|
287
|
+
return {
|
|
288
|
+
"type": "taskItem",
|
|
289
|
+
"attrs": {
|
|
290
|
+
"localId": _generate_local_id(),
|
|
291
|
+
"state": "DONE" if checked else "TODO",
|
|
292
|
+
},
|
|
293
|
+
"content": item_content,
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
def _process_list_item(item: dict[str, Any]) -> AdfNode:
|
|
298
|
+
item_content: list[AdfNode] = []
|
|
299
|
+
current_paragraph_tokens: list[dict[str, Any]] = []
|
|
300
|
+
|
|
301
|
+
inline_types = {"text", "emphasis", "strong", "strikethrough", "link", "codespan", "block_text"}
|
|
302
|
+
|
|
303
|
+
for token in item.get("children", []):
|
|
304
|
+
token_type = token.get("type", "")
|
|
305
|
+
|
|
306
|
+
if token_type in inline_types:
|
|
307
|
+
current_paragraph_tokens.append(token)
|
|
308
|
+
else:
|
|
309
|
+
if current_paragraph_tokens:
|
|
310
|
+
item_content.append({
|
|
311
|
+
"type": "paragraph",
|
|
312
|
+
"content": _inline_to_adf(current_paragraph_tokens),
|
|
313
|
+
})
|
|
314
|
+
current_paragraph_tokens = []
|
|
315
|
+
|
|
316
|
+
if token_type == "list":
|
|
317
|
+
list_items = token.get("children", [])
|
|
318
|
+
if _is_task_list(list_items):
|
|
319
|
+
item_content.append({
|
|
320
|
+
"type": "taskList",
|
|
321
|
+
"attrs": {"localId": _generate_local_id()},
|
|
322
|
+
"content": [_process_task_item(li) for li in list_items],
|
|
323
|
+
})
|
|
324
|
+
else:
|
|
325
|
+
is_ordered = token.get("attrs", {}).get("ordered", False)
|
|
326
|
+
start = token.get("attrs", {}).get("start", 1)
|
|
327
|
+
list_node: AdfNode = {
|
|
328
|
+
"type": "orderedList" if is_ordered else "bulletList",
|
|
329
|
+
"content": [_process_list_item(li) for li in list_items],
|
|
330
|
+
}
|
|
331
|
+
if is_ordered:
|
|
332
|
+
list_node["attrs"] = {"order": start}
|
|
333
|
+
item_content.append(list_node)
|
|
334
|
+
else:
|
|
335
|
+
processed = _tokens_to_adf([token])
|
|
336
|
+
item_content.extend(processed)
|
|
337
|
+
|
|
338
|
+
if current_paragraph_tokens:
|
|
339
|
+
item_content.append({
|
|
340
|
+
"type": "paragraph",
|
|
341
|
+
"content": _inline_to_adf(current_paragraph_tokens),
|
|
342
|
+
})
|
|
343
|
+
|
|
344
|
+
return {
|
|
345
|
+
"type": "listItem",
|
|
346
|
+
"content": item_content,
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
def _process_table_cell_content(children: list[dict[str, Any]]) -> list[AdfNode]:
|
|
351
|
+
if not children:
|
|
352
|
+
return [{"type": "paragraph", "content": [{"type": "text", "text": " "}]}]
|
|
353
|
+
|
|
354
|
+
has_image = any(child.get("type") == "image" for child in children)
|
|
355
|
+
if has_image:
|
|
356
|
+
return _process_paragraph(children)
|
|
357
|
+
|
|
358
|
+
inline_content = _inline_to_adf(children)
|
|
359
|
+
if not inline_content:
|
|
360
|
+
return [{"type": "paragraph", "content": [{"type": "text", "text": " "}]}]
|
|
361
|
+
|
|
362
|
+
return [{"type": "paragraph", "content": inline_content}]
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
def _process_table(token: dict[str, Any]) -> AdfNode:
|
|
366
|
+
content: list[AdfNode] = []
|
|
367
|
+
|
|
368
|
+
for child in token.get("children", []):
|
|
369
|
+
child_type = child.get("type", "")
|
|
370
|
+
|
|
371
|
+
if child_type == "table_head":
|
|
372
|
+
headers: list[AdfNode] = []
|
|
373
|
+
for cell in child.get("children", []):
|
|
374
|
+
if cell.get("type") == "table_cell":
|
|
375
|
+
cell_content = _process_table_cell_content(cell.get("children", []))
|
|
376
|
+
headers.append({
|
|
377
|
+
"type": "tableHeader",
|
|
378
|
+
"content": cell_content,
|
|
379
|
+
})
|
|
380
|
+
if headers:
|
|
381
|
+
content.append({"type": "tableRow", "content": headers})
|
|
382
|
+
|
|
383
|
+
elif child_type == "table_body":
|
|
384
|
+
for row in child.get("children", []):
|
|
385
|
+
cells: list[AdfNode] = []
|
|
386
|
+
for cell in row.get("children", []):
|
|
387
|
+
if cell.get("type") == "table_cell":
|
|
388
|
+
cell_content = _process_table_cell_content(cell.get("children", []))
|
|
389
|
+
cells.append({"type": "tableCell", "content": cell_content})
|
|
390
|
+
if cells:
|
|
391
|
+
content.append({"type": "tableRow", "content": cells})
|
|
392
|
+
|
|
393
|
+
return {"type": "table", "content": content}
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
def _tokens_to_adf(tokens: list[dict[str, Any]] | None) -> list[AdfNode]:
|
|
397
|
+
if not tokens:
|
|
398
|
+
return []
|
|
399
|
+
|
|
400
|
+
result: list[AdfNode] = []
|
|
401
|
+
|
|
402
|
+
for token in tokens:
|
|
403
|
+
token_type = token.get("type", "")
|
|
404
|
+
|
|
405
|
+
if token_type == "paragraph":
|
|
406
|
+
result.extend(_process_paragraph(token.get("children", [])))
|
|
407
|
+
|
|
408
|
+
elif token_type == "heading":
|
|
409
|
+
level = token.get("attrs", {}).get("level", 1)
|
|
410
|
+
result.append({
|
|
411
|
+
"type": "heading",
|
|
412
|
+
"attrs": {"level": level},
|
|
413
|
+
"content": _inline_to_adf(token.get("children", [])),
|
|
414
|
+
})
|
|
415
|
+
|
|
416
|
+
elif token_type == "list":
|
|
417
|
+
list_items = token.get("children", [])
|
|
418
|
+
if _is_task_list(list_items):
|
|
419
|
+
result.append({
|
|
420
|
+
"type": "taskList",
|
|
421
|
+
"attrs": {"localId": _generate_local_id()},
|
|
422
|
+
"content": [_process_task_item(item) for item in list_items],
|
|
423
|
+
})
|
|
424
|
+
else:
|
|
425
|
+
is_ordered = token.get("attrs", {}).get("ordered", False)
|
|
426
|
+
start = token.get("attrs", {}).get("start", 1)
|
|
427
|
+
list_node: AdfNode = {
|
|
428
|
+
"type": "orderedList" if is_ordered else "bulletList",
|
|
429
|
+
"content": [_process_list_item(item) for item in list_items],
|
|
430
|
+
}
|
|
431
|
+
if is_ordered:
|
|
432
|
+
list_node["attrs"] = {"order": start}
|
|
433
|
+
result.append(list_node)
|
|
434
|
+
|
|
435
|
+
elif token_type == "block_code":
|
|
436
|
+
lang = token.get("attrs", {}).get("info", "") or "text"
|
|
437
|
+
raw_text = token.get("raw", "")
|
|
438
|
+
if raw_text.endswith("\n"):
|
|
439
|
+
raw_text = raw_text[:-1]
|
|
440
|
+
result.append({
|
|
441
|
+
"type": "codeBlock",
|
|
442
|
+
"attrs": {"language": lang},
|
|
443
|
+
"content": [{"type": "text", "text": raw_text}],
|
|
444
|
+
})
|
|
445
|
+
|
|
446
|
+
elif token_type == "block_quote":
|
|
447
|
+
result.append({
|
|
448
|
+
"type": "blockquote",
|
|
449
|
+
"content": _tokens_to_adf(token.get("children", [])),
|
|
450
|
+
})
|
|
451
|
+
|
|
452
|
+
elif token_type == "thematic_break":
|
|
453
|
+
result.append({"type": "rule"})
|
|
454
|
+
|
|
455
|
+
elif token_type == "table":
|
|
456
|
+
result.append(_process_table(token))
|
|
457
|
+
|
|
458
|
+
return result
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
def markdown_to_adf(markdown: str) -> AdfDocument:
|
|
462
|
+
md = mistune.create_markdown(
|
|
463
|
+
renderer=None,
|
|
464
|
+
plugins=["strikethrough", "table", "task_lists"],
|
|
465
|
+
)
|
|
466
|
+
result = md(markdown)
|
|
467
|
+
tokens: list[dict[str, Any]] = result if isinstance(result, list) else []
|
|
468
|
+
|
|
469
|
+
return {
|
|
470
|
+
"version": 1,
|
|
471
|
+
"type": "doc",
|
|
472
|
+
"content": _tokens_to_adf(tokens),
|
|
473
|
+
}
|
marklassian/types.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
from typing import Any, Literal, Required, TypedDict
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class AdfMark(TypedDict, total=False):
|
|
5
|
+
type: Required[str]
|
|
6
|
+
attrs: dict[str, Any]
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class AdfNode(TypedDict, total=False):
|
|
10
|
+
type: Required[str]
|
|
11
|
+
attrs: dict[str, Any]
|
|
12
|
+
content: list["AdfNode"]
|
|
13
|
+
marks: list[AdfMark]
|
|
14
|
+
text: str
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class AdfDocument(TypedDict):
|
|
18
|
+
version: Literal[1]
|
|
19
|
+
type: Literal["doc"]
|
|
20
|
+
content: list[AdfNode]
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: marklassian
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Convert Markdown to Atlassian Document Format (ADF)
|
|
5
|
+
Project-URL: Homepage, https://github.com/mbroton/marklassian-py
|
|
6
|
+
Project-URL: Repository, https://github.com/mbroton/marklassian-py
|
|
7
|
+
Author-email: Michał Brotoń <michal@broton.dev>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: adf,atlassian,confluence,jira,markdown
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Topic :: Text Processing :: Markup :: Markdown
|
|
20
|
+
Requires-Python: >=3.10
|
|
21
|
+
Requires-Dist: mistune>=3.0.0
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# marklassian-py
|
|
25
|
+
|
|
26
|
+
A lightweight Python library that converts Markdown to the [Atlassian Document Format (ADF)](https://developer.atlassian.com/cloud/jira/platform/apis/document/structure/). Built for easy integration with Atlassian products like Jira and Confluence.
|
|
27
|
+
|
|
28
|
+
This is a Python port of the excellent [marklassian](https://github.com/jamsinclair/marklassian) JavaScript library by [@jamsinclair](https://github.com/jamsinclair).
|
|
29
|
+
|
|
30
|
+
## Features
|
|
31
|
+
|
|
32
|
+
- Convert Markdown to ADF with a single function call
|
|
33
|
+
- Support for common Markdown syntax including GFM task lists
|
|
34
|
+
- Minimal dependencies (only [mistune](https://github.com/lepture/mistune))
|
|
35
|
+
- Full type hints for IDE support
|
|
36
|
+
- Python 3.10+
|
|
37
|
+
|
|
38
|
+
## Installation
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pip install marklassian
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Or with uv:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
uv add marklassian
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Usage
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
from marklassian import markdown_to_adf
|
|
54
|
+
|
|
55
|
+
markdown = "# Hello World\n\nThis is **bold** and *italic* text."
|
|
56
|
+
adf = markdown_to_adf(markdown)
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
The result is a dictionary that can be serialized to JSON:
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
import json
|
|
63
|
+
print(json.dumps(adf, indent=2))
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Supported Markdown Features
|
|
67
|
+
|
|
68
|
+
- Headings (H1-H6)
|
|
69
|
+
- Paragraphs and line breaks
|
|
70
|
+
- Emphasis (bold, italic, strikethrough)
|
|
71
|
+
- Links and images
|
|
72
|
+
- Inline code and code blocks with language support
|
|
73
|
+
- Ordered and unordered lists with nesting
|
|
74
|
+
- Blockquotes
|
|
75
|
+
- Horizontal rules
|
|
76
|
+
- Tables
|
|
77
|
+
- Task lists (GitHub Flavored Markdown)
|
|
78
|
+
|
|
79
|
+
## API Reference
|
|
80
|
+
|
|
81
|
+
### `markdown_to_adf(markdown: str) -> AdfDocument`
|
|
82
|
+
|
|
83
|
+
Converts a Markdown string to an ADF document object.
|
|
84
|
+
|
|
85
|
+
### Types
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
class AdfMark(TypedDict, total=False):
|
|
89
|
+
type: Required[str]
|
|
90
|
+
attrs: dict[str, Any]
|
|
91
|
+
|
|
92
|
+
class AdfNode(TypedDict, total=False):
|
|
93
|
+
type: Required[str]
|
|
94
|
+
attrs: dict[str, Any]
|
|
95
|
+
content: list[AdfNode]
|
|
96
|
+
marks: list[AdfMark]
|
|
97
|
+
text: str
|
|
98
|
+
|
|
99
|
+
class AdfDocument(TypedDict):
|
|
100
|
+
version: Literal[1]
|
|
101
|
+
type: Literal["doc"]
|
|
102
|
+
content: list[AdfNode]
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Caveats
|
|
106
|
+
|
|
107
|
+
This library aims to provide a lightweight and mostly accurate conversion from Markdown to ADF.
|
|
108
|
+
|
|
109
|
+
For complex Markdown documents or strict ADF conformance requirements, consider using the official Atlassian libraries. Note that those are heavier dependencies.
|
|
110
|
+
|
|
111
|
+
## References
|
|
112
|
+
|
|
113
|
+
- [Atlassian Document Format Reference](https://developer.atlassian.com/cloud/jira/platform/apis/document/structure/)
|
|
114
|
+
- [ADF Interactive Builder](https://developer.atlassian.com/cloud/jira/platform/apis/document/playground/)
|
|
115
|
+
- [Original marklassian (JavaScript)](https://github.com/jamsinclair/marklassian)
|
|
116
|
+
|
|
117
|
+
## Credits
|
|
118
|
+
|
|
119
|
+
This library is a Python port of [marklassian](https://github.com/jamsinclair/marklassian) by [Jamie Sinclair](https://github.com/jamsinclair). All credit for the original implementation and conversion logic goes to them.
|
|
120
|
+
|
|
121
|
+
## License
|
|
122
|
+
|
|
123
|
+
MIT - see [LICENSE](LICENSE) for details.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
marklassian/__init__.py,sha256=sLhIIbLlcdJr3Tn86LZiuBcu2Uk7OBLdyF0hqy_6Eo4,233
|
|
2
|
+
marklassian/converter.py,sha256=XQ6ovSItjlXbyYO-wgQdPGdqXP3fQxz5WkN1Rgs3b2U,15793
|
|
3
|
+
marklassian/types.py,sha256=LWGbm1mFgsmcgu5tZKPa1DDio0IZKcLYsZKFzZlahWE,411
|
|
4
|
+
marklassian-0.1.0.dist-info/METADATA,sha256=8OPPX52eb39cQj2EMZ2IFj86G1PwlHhb4kBs9upbhMs,3717
|
|
5
|
+
marklassian-0.1.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
6
|
+
marklassian-0.1.0.dist-info/licenses/LICENSE,sha256=uAEkwBS3-lesdvgNb0VFQ4-5HKj-LgwsNE8tx071-3w,1072
|
|
7
|
+
marklassian-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Michał Brotoń
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|