markitdown-mdzip 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.
- markitdown_mdzip/__init__.py +33 -0
- markitdown_mdzip/__main__.py +51 -0
- markitdown_mdzip/_converter.py +110 -0
- markitdown_mdzip/_to_mdz.py +135 -0
- markitdown_mdzip/_version.py +1 -0
- markitdown_mdzip-0.1.0.dist-info/METADATA +150 -0
- markitdown_mdzip-0.1.0.dist-info/RECORD +11 -0
- markitdown_mdzip-0.1.0.dist-info/WHEEL +5 -0
- markitdown_mdzip-0.1.0.dist-info/entry_points.txt +5 -0
- markitdown_mdzip-0.1.0.dist-info/licenses/LICENSE +201 -0
- markitdown_mdzip-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"""markitdown-mdzip — a MarkItDown plugin that converts MDZip (.mdz) archives to Markdown.
|
|
2
|
+
|
|
3
|
+
Install it next to MarkItDown and enable plugins:
|
|
4
|
+
|
|
5
|
+
markitdown --use-plugins guide.mdz
|
|
6
|
+
|
|
7
|
+
from markitdown import MarkItDown
|
|
8
|
+
md = MarkItDown(enable_plugins=True)
|
|
9
|
+
print(md.convert("guide.mdz").markdown)
|
|
10
|
+
|
|
11
|
+
It also goes the other way, from anything MarkItDown reads to .mdz, keeping the images:
|
|
12
|
+
|
|
13
|
+
markitdown-mdz report.docx # writes report.mdz
|
|
14
|
+
|
|
15
|
+
from markitdown_mdzip import convert_to_mdz
|
|
16
|
+
convert_to_mdz("report.docx").save("report.mdz")
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from ._converter import MdzConverter, register_converters
|
|
20
|
+
from ._to_mdz import MdzConversion, convert_to_mdz
|
|
21
|
+
from ._version import __version__
|
|
22
|
+
|
|
23
|
+
__plugin_interface_version__ = 1
|
|
24
|
+
"""The MarkItDown plugin interface this plugin implements."""
|
|
25
|
+
|
|
26
|
+
__all__ = [
|
|
27
|
+
"__version__",
|
|
28
|
+
"__plugin_interface_version__",
|
|
29
|
+
"MdzConverter",
|
|
30
|
+
"register_converters",
|
|
31
|
+
"convert_to_mdz",
|
|
32
|
+
"MdzConversion",
|
|
33
|
+
]
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""markitdown-mdz: convert a file to an .mdz archive with MarkItDown, keeping its images."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import os
|
|
7
|
+
import sys
|
|
8
|
+
from typing import List, Optional
|
|
9
|
+
|
|
10
|
+
from markitdown import MarkItDown
|
|
11
|
+
|
|
12
|
+
from ._to_mdz import convert_to_mdz
|
|
13
|
+
from ._version import __version__
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def main(argv: Optional[List[str]] = None) -> int:
|
|
17
|
+
parser = argparse.ArgumentParser(
|
|
18
|
+
prog="markitdown-mdz",
|
|
19
|
+
description="Convert a file (Word, PowerPoint, HTML, PDF, ...) to an .mdz archive with MarkItDown. "
|
|
20
|
+
"Images are kept as files in the archive.",
|
|
21
|
+
)
|
|
22
|
+
parser.add_argument("source", help="file path or URL to convert")
|
|
23
|
+
parser.add_argument("-o", "--output", help="output .mdz path (default: the source name with .mdz)")
|
|
24
|
+
parser.add_argument("--title", help="archive title (default: the document's title or file name)")
|
|
25
|
+
parser.add_argument("-p", "--use-plugins", action="store_true", help="enable MarkItDown's 3rd-party plugins")
|
|
26
|
+
parser.add_argument("-f", "--force", action="store_true", help="overwrite the output file if it exists")
|
|
27
|
+
parser.add_argument("--version", action="version", version=f"%(prog)s {__version__}")
|
|
28
|
+
args = parser.parse_args(argv)
|
|
29
|
+
|
|
30
|
+
output = args.output
|
|
31
|
+
if not output:
|
|
32
|
+
name = os.path.basename(args.source.split("?")[0].rstrip("/")) or "document"
|
|
33
|
+
output = os.path.splitext(name)[0] + ".mdz"
|
|
34
|
+
if os.path.abspath(output) == os.path.abspath(args.source):
|
|
35
|
+
parser.error("the output would overwrite the source")
|
|
36
|
+
if os.path.exists(output) and not args.force:
|
|
37
|
+
parser.error(f"{output} already exists (use --force to overwrite)")
|
|
38
|
+
|
|
39
|
+
try:
|
|
40
|
+
result = convert_to_mdz(args.source, markitdown=MarkItDown(enable_plugins=args.use_plugins), title=args.title)
|
|
41
|
+
except Exception as exc: # MarkItDown raises several unrelated exception types
|
|
42
|
+
print(f"markitdown-mdz: {exc}", file=sys.stderr)
|
|
43
|
+
return 1
|
|
44
|
+
result.save(output)
|
|
45
|
+
images = len(result.images)
|
|
46
|
+
print(f"Wrote {output}: {result.entry_point} + {images} image{'' if images == 1 else 's'}")
|
|
47
|
+
return 0
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
if __name__ == "__main__":
|
|
51
|
+
sys.exit(main())
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"""The MarkItDown converter for .mdz archives."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import base64
|
|
6
|
+
import mimetypes
|
|
7
|
+
import re
|
|
8
|
+
from typing import Any, BinaryIO, List, Optional
|
|
9
|
+
|
|
10
|
+
import mdzip_core
|
|
11
|
+
from markitdown import DocumentConverter, DocumentConverterResult, MarkItDown, StreamInfo
|
|
12
|
+
|
|
13
|
+
ACCEPTED_EXTENSIONS = [".mdz"]
|
|
14
|
+
ACCEPTED_MIME_TYPES = ["application/vnd.mdzip"]
|
|
15
|
+
"""The spec's proposed media type (MDZip spec Section 10)."""
|
|
16
|
+
|
|
17
|
+
DOCUMENTS_OPTIONS = ("auto", "entry", "all")
|
|
18
|
+
|
|
19
|
+
# An inline image:  or . Reference-style images and
|
|
20
|
+
# raw <img> tags are left as written.
|
|
21
|
+
_IMAGE = re.compile(r'(!\[[^\]\n]*\]\()\s*(<[^>\n]*>|[^)\s]+)((?:\s+(?:"[^"\n]*"|\'[^\'\n]*\'))?\s*\))')
|
|
22
|
+
|
|
23
|
+
# A leading "# Title" line, used when the manifest has no title.
|
|
24
|
+
_HEADING = re.compile(r"^\s{0,3}#\s+(.+?)\s*#*\s*$", re.MULTILINE)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def register_converters(markitdown: MarkItDown, **kwargs: Any) -> None:
|
|
28
|
+
"""Called by MarkItDown when plugins are enabled."""
|
|
29
|
+
# Priority 0 (PRIORITY_SPECIFIC_FILE_FORMAT) runs before MarkItDown's generic ZIP
|
|
30
|
+
# converter, which would otherwise list the archive's files.
|
|
31
|
+
markitdown.register_converter(MdzConverter(), priority=0.0)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class MdzConverter(DocumentConverter):
|
|
35
|
+
"""Converts an MDZip archive to Markdown.
|
|
36
|
+
|
|
37
|
+
The output is the archive's entry point (MDZip spec Section 5.5). For a
|
|
38
|
+
project-mode archive, the output also has every other Markdown document in it,
|
|
39
|
+
each one introduced by an ``<!-- mdz:document path="..." -->`` comment. Support
|
|
40
|
+
files (README, AGENTS, LICENSE, CHANGELOG and CONTRIBUTING at the root) are left
|
|
41
|
+
out unless one of them is the entry point.
|
|
42
|
+
|
|
43
|
+
Options, passed as ``convert()`` keyword arguments:
|
|
44
|
+
|
|
45
|
+
- ``mdz_documents``: ``"auto"`` (default; ``"all"`` for project mode, otherwise
|
|
46
|
+
``"entry"``), ``"entry"``, or ``"all"``.
|
|
47
|
+
- ``keep_data_uris``: embed images stored in the archive as ``data:`` URIs,
|
|
48
|
+
instead of leaving their archive paths, which don't resolve once the Markdown
|
|
49
|
+
is out of the archive. This is the same option MarkItDown's DOCX and PPTX
|
|
50
|
+
converters use.
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
def accepts(self, file_stream: BinaryIO, stream_info: StreamInfo, **kwargs: Any) -> bool:
|
|
54
|
+
extension = (stream_info.extension or "").lower()
|
|
55
|
+
mimetype = (stream_info.mimetype or "").lower()
|
|
56
|
+
return extension in ACCEPTED_EXTENSIONS or any(mimetype.startswith(m) for m in ACCEPTED_MIME_TYPES)
|
|
57
|
+
|
|
58
|
+
def convert(self, file_stream: BinaryIO, stream_info: StreamInfo, **kwargs: Any) -> DocumentConverterResult:
|
|
59
|
+
documents = kwargs.get("mdz_documents", "auto")
|
|
60
|
+
if documents not in DOCUMENTS_OPTIONS:
|
|
61
|
+
raise ValueError(f"mdz_documents must be one of {', '.join(DOCUMENTS_OPTIONS)}, not {documents!r}")
|
|
62
|
+
keep_data_uris = bool(kwargs.get("keep_data_uris", False))
|
|
63
|
+
|
|
64
|
+
archive = mdzip_core.MdzArchive(file_stream.read(), path=stream_info.local_path)
|
|
65
|
+
entry = archive.entry_point
|
|
66
|
+
if documents == "auto":
|
|
67
|
+
documents = "all" if archive.mode == "project" else "entry"
|
|
68
|
+
|
|
69
|
+
paths = [entry]
|
|
70
|
+
if documents == "all":
|
|
71
|
+
paths += [p for p in archive.documents if p != entry and not mdzip_core.is_support_file(p)]
|
|
72
|
+
|
|
73
|
+
parts: List[str] = []
|
|
74
|
+
for path in paths:
|
|
75
|
+
text = archive.read_text(path)
|
|
76
|
+
if keep_data_uris:
|
|
77
|
+
text = _embed_images(archive, path, text)
|
|
78
|
+
if len(paths) > 1:
|
|
79
|
+
text = f'<!-- mdz:document path="{_attr(path)}" -->\n\n{text.strip()}'
|
|
80
|
+
parts.append(text.strip())
|
|
81
|
+
|
|
82
|
+
markdown = "\n\n".join(parts) + "\n"
|
|
83
|
+
return DocumentConverterResult(markdown=markdown, title=archive.title or _first_heading(markdown))
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _embed_images(archive: mdzip_core.MdzArchive, document: str, text: str) -> str:
|
|
87
|
+
"""Replace archive image paths in ``text`` with data: URIs."""
|
|
88
|
+
|
|
89
|
+
def replace(match: "re.Match[str]") -> str:
|
|
90
|
+
dest = match.group(2)
|
|
91
|
+
bare = dest[1:-1] if dest.startswith("<") else dest
|
|
92
|
+
target = archive.resolve_link(document, bare)
|
|
93
|
+
if target.kind != "internal" or target.entry is None:
|
|
94
|
+
return match.group(0)
|
|
95
|
+
mimetype = mimetypes.guess_type(target.entry)[0]
|
|
96
|
+
if not mimetype or not mimetype.startswith("image/"):
|
|
97
|
+
return match.group(0)
|
|
98
|
+
data = base64.b64encode(archive.read_bytes(target.entry)).decode("ascii")
|
|
99
|
+
return f"{match.group(1)}data:{mimetype};base64,{data}{match.group(3)}"
|
|
100
|
+
|
|
101
|
+
return _IMAGE.sub(replace, text)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _first_heading(markdown: str) -> Optional[str]:
|
|
105
|
+
match = _HEADING.search(markdown)
|
|
106
|
+
return match.group(1) if match else None
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def _attr(value: str) -> str:
|
|
110
|
+
return value.replace("&", "&").replace('"', """).replace("--", "--")
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"""Convert any file MarkItDown understands into an .mdz archive, keeping its images."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import base64
|
|
6
|
+
import binascii
|
|
7
|
+
import hashlib
|
|
8
|
+
import os
|
|
9
|
+
import re
|
|
10
|
+
from dataclasses import dataclass, field
|
|
11
|
+
from typing import Any, BinaryIO, Dict, List, Optional, Union
|
|
12
|
+
|
|
13
|
+
import mdzip_core
|
|
14
|
+
from markitdown import MarkItDown, StreamInfo
|
|
15
|
+
|
|
16
|
+
from ._converter import _first_heading
|
|
17
|
+
|
|
18
|
+
# A base64 image data: URI, as MarkItDown writes them with keep_data_uris=True.
|
|
19
|
+
_DATA_URI = re.compile(r"data:(image/[A-Za-z0-9.+-]+);base64,([A-Za-z0-9+/]+={0,2})")
|
|
20
|
+
|
|
21
|
+
_EXTENSIONS = {
|
|
22
|
+
"image/png": ".png",
|
|
23
|
+
"image/jpeg": ".jpg",
|
|
24
|
+
"image/jpg": ".jpg",
|
|
25
|
+
"image/gif": ".gif",
|
|
26
|
+
"image/webp": ".webp",
|
|
27
|
+
"image/svg+xml": ".svg",
|
|
28
|
+
"image/bmp": ".bmp",
|
|
29
|
+
"image/tiff": ".tif",
|
|
30
|
+
"image/x-emf": ".emf",
|
|
31
|
+
"image/x-wmf": ".wmf",
|
|
32
|
+
"image/avif": ".avif",
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
Source = Union[str, "os.PathLike[str]", BinaryIO]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@dataclass
|
|
39
|
+
class MdzConversion:
|
|
40
|
+
"""The result of :func:`convert_to_mdz`."""
|
|
41
|
+
|
|
42
|
+
data: bytes
|
|
43
|
+
"""The .mdz archive bytes."""
|
|
44
|
+
markdown: str
|
|
45
|
+
"""The entry document's Markdown, with image links pointing into the archive."""
|
|
46
|
+
entry_point: str
|
|
47
|
+
title: Optional[str]
|
|
48
|
+
images: List[str] = field(default_factory=list)
|
|
49
|
+
"""Archive paths of the images that were extracted, in order of first appearance."""
|
|
50
|
+
|
|
51
|
+
def save(self, path: Union[str, "os.PathLike[str]"]) -> None:
|
|
52
|
+
with open(path, "wb") as f:
|
|
53
|
+
f.write(self.data)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def convert_to_mdz(
|
|
57
|
+
source: Source,
|
|
58
|
+
*,
|
|
59
|
+
markitdown: Optional[MarkItDown] = None,
|
|
60
|
+
stream_info: Optional[StreamInfo] = None,
|
|
61
|
+
title: Optional[str] = None,
|
|
62
|
+
entry_point: Optional[str] = None,
|
|
63
|
+
images_dir: str = "images",
|
|
64
|
+
**kwargs: Any,
|
|
65
|
+
) -> MdzConversion:
|
|
66
|
+
"""Convert ``source`` (a path, URL or binary stream) with MarkItDown and package the result.
|
|
67
|
+
|
|
68
|
+
MarkItDown is asked to keep images as ``data:`` URIs (``keep_data_uris=True``);
|
|
69
|
+
each one is written to ``images_dir`` in the archive, named in order of first
|
|
70
|
+
appearance (``image-001.png``, ...), with duplicates stored once, and the link is
|
|
71
|
+
rewritten to point at it. Formats whose MarkItDown converter emits no image data
|
|
72
|
+
(PDF, for example) give an archive with the text only.
|
|
73
|
+
|
|
74
|
+
``entry_point`` defaults to the source's file name with ``.md`` (``report.docx`` ->
|
|
75
|
+
``report.md``), or ``index.md`` if there's no usable name. ``title`` defaults to the
|
|
76
|
+
title MarkItDown found, then the first ``#`` heading, then the file name. Other
|
|
77
|
+
keyword arguments go to ``MarkItDown.convert``.
|
|
78
|
+
"""
|
|
79
|
+
md = markitdown or MarkItDown()
|
|
80
|
+
kwargs.setdefault("keep_data_uris", True)
|
|
81
|
+
if isinstance(source, (str, os.PathLike)):
|
|
82
|
+
result = md.convert(source, stream_info=stream_info, **kwargs)
|
|
83
|
+
name = os.path.basename(os.fspath(source).split("?")[0].rstrip("/"))
|
|
84
|
+
else:
|
|
85
|
+
result = md.convert_stream(source, stream_info=stream_info, **kwargs)
|
|
86
|
+
name = (stream_info.filename if stream_info else None) or getattr(source, "name", None) or ""
|
|
87
|
+
name = os.path.basename(str(name))
|
|
88
|
+
|
|
89
|
+
stem = os.path.splitext(name)[0]
|
|
90
|
+
entry = entry_point or _entry_name(stem)
|
|
91
|
+
images_dir = images_dir.strip("/")
|
|
92
|
+
markdown, files = _extract_images(result.markdown, images_dir)
|
|
93
|
+
doc_title = title or result.title or _first_heading(markdown) or stem or None
|
|
94
|
+
|
|
95
|
+
contents: Dict[str, Union[str, bytes]] = {entry: markdown}
|
|
96
|
+
contents.update(files)
|
|
97
|
+
data = mdzip_core.create(contents, title=doc_title, entry_point=entry)
|
|
98
|
+
return MdzConversion(data=data, markdown=markdown, entry_point=entry, title=doc_title, images=list(files))
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def _entry_name(stem: str) -> str:
|
|
102
|
+
"""``<stem>.md`` if that's a valid root-level archive path, else ``index.md``."""
|
|
103
|
+
candidate = f"{stem.strip()}.md"
|
|
104
|
+
if not stem.strip() or "/" in candidate:
|
|
105
|
+
return "index.md"
|
|
106
|
+
try:
|
|
107
|
+
mdzip_core.validate_path(candidate)
|
|
108
|
+
except mdzip_core.MdzError:
|
|
109
|
+
return "index.md"
|
|
110
|
+
if mdzip_core.is_support_file(candidate):
|
|
111
|
+
# A support-file name (README.md, ...) would read as package documentation.
|
|
112
|
+
return "index.md"
|
|
113
|
+
return candidate
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def _extract_images(markdown: str, images_dir: str):
|
|
117
|
+
files: Dict[str, bytes] = {}
|
|
118
|
+
by_hash: Dict[str, str] = {}
|
|
119
|
+
|
|
120
|
+
def replace(match: "re.Match[str]") -> str:
|
|
121
|
+
mimetype = match.group(1).lower()
|
|
122
|
+
try:
|
|
123
|
+
data = base64.b64decode(match.group(2), validate=True)
|
|
124
|
+
except (binascii.Error, ValueError):
|
|
125
|
+
return match.group(0)
|
|
126
|
+
digest = hashlib.sha256(data).hexdigest()
|
|
127
|
+
path = by_hash.get(digest)
|
|
128
|
+
if path is None:
|
|
129
|
+
extension = _EXTENSIONS.get(mimetype, ".bin")
|
|
130
|
+
path = f"{images_dir}/image-{len(by_hash) + 1:03d}{extension}" if images_dir else f"image-{len(by_hash) + 1:03d}{extension}"
|
|
131
|
+
by_hash[digest] = path
|
|
132
|
+
files[path] = data
|
|
133
|
+
return path
|
|
134
|
+
|
|
135
|
+
return _DATA_URI.sub(replace, markdown), files
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.1.0"
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: markitdown-mdzip
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: MarkItDown plugin for MDZip (.mdz): convert .mdz archives to Markdown, and Word, PowerPoint, HTML and more to .mdz with the images kept.
|
|
5
|
+
License-Expression: Apache-2.0
|
|
6
|
+
Project-URL: Homepage, https://mdzip.org
|
|
7
|
+
Project-URL: Specification, https://github.com/mdzip-project/mdzip-spec
|
|
8
|
+
Project-URL: Source, https://github.com/mdzip-project/mdzip-markitdown
|
|
9
|
+
Keywords: markitdown,markitdown-plugin,mdzip,mdz,markdown
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
14
|
+
Classifier: Topic :: Text Processing :: Markup :: Markdown
|
|
15
|
+
Requires-Python: >=3.10
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Requires-Dist: markitdown>=0.1.0
|
|
19
|
+
Requires-Dist: mdzip-core>=0.1.0
|
|
20
|
+
Provides-Extra: docx
|
|
21
|
+
Requires-Dist: markitdown[docx]; extra == "docx"
|
|
22
|
+
Provides-Extra: pptx
|
|
23
|
+
Requires-Dist: markitdown[pptx]; extra == "pptx"
|
|
24
|
+
Provides-Extra: pdf
|
|
25
|
+
Requires-Dist: markitdown[pdf]; extra == "pdf"
|
|
26
|
+
Provides-Extra: all
|
|
27
|
+
Requires-Dist: markitdown[all]; extra == "all"
|
|
28
|
+
Dynamic: license-file
|
|
29
|
+
|
|
30
|
+
# markitdown-mdzip
|
|
31
|
+
|
|
32
|
+
A [MarkItDown](https://github.com/microsoft/markitdown) plugin that converts **MDZip** (`.mdz`) archives to Markdown, and a `markitdown-mdz` command that goes the other way: Word, PowerPoint, HTML and more to `.mdz`, with the images kept.
|
|
33
|
+
|
|
34
|
+
An `.mdz` file is one or more Markdown documents and their images, packaged in a single ZIP file ([specification](https://github.com/mdzip-project/mdzip-spec)). Without this plugin, MarkItDown treats an `.mdz` as a plain ZIP and lists every file in it, `manifest.json` included. With it, you get the document itself.
|
|
35
|
+
|
|
36
|
+
## Install
|
|
37
|
+
|
|
38
|
+
```sh
|
|
39
|
+
pip install markitdown-mdzip
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Use
|
|
43
|
+
|
|
44
|
+
Plugins are off by default in MarkItDown, so turn them on:
|
|
45
|
+
|
|
46
|
+
```sh
|
|
47
|
+
markitdown --use-plugins guide.mdz
|
|
48
|
+
markitdown --list-plugins # shows: mdzip
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
from markitdown import MarkItDown
|
|
53
|
+
|
|
54
|
+
md = MarkItDown(enable_plugins=True)
|
|
55
|
+
result = md.convert("guide.mdz")
|
|
56
|
+
print(result.title)
|
|
57
|
+
print(result.markdown)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## What you get
|
|
61
|
+
|
|
62
|
+
- **A document-mode archive** (the usual kind) gives you its entry point: the main document, as the MDZip spec defines it.
|
|
63
|
+
- **A project-mode archive** gives you every document. The entry point comes first. Each document starts with a comment naming its path, so the boundaries survive:
|
|
64
|
+
|
|
65
|
+
```markdown
|
|
66
|
+
<!-- mdz:document path="guide/setup.md" -->
|
|
67
|
+
|
|
68
|
+
# Setup
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Support files at the archive root (`README`, `AGENTS`, `LICENSE`, `CHANGELOG`, `CONTRIBUTING`) describe the package rather than its content. They're left out unless one of them is the entry point.
|
|
72
|
+
- **The title** comes from the archive's manifest, or else from the first `#` heading.
|
|
73
|
+
|
|
74
|
+
### Options
|
|
75
|
+
|
|
76
|
+
Pass these to `convert()`:
|
|
77
|
+
|
|
78
|
+
| Option | Values | Default |
|
|
79
|
+
|---|---|---|
|
|
80
|
+
| `mdz_documents` | `"auto"`, `"entry"`, `"all"` | `"auto"`: `"all"` for project mode, otherwise `"entry"` |
|
|
81
|
+
| `keep_data_uris` | `True` / `False` | `False` |
|
|
82
|
+
|
|
83
|
+
By default, image links keep their paths inside the archive (`images/logo.png`), which don't resolve once the Markdown is outside it. `keep_data_uris=True` embeds those images as `data:` URIs, so the output stands alone. It's the same option MarkItDown's Word and PowerPoint converters use.
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
md.convert("guide.mdz", keep_data_uris=True, mdz_documents="all")
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Errors
|
|
90
|
+
|
|
91
|
+
The plugin follows the spec. It won't guess an entry point: an archive with two candidate documents at its root and no manifest isn't converted by this plugin. The same goes for invalid archives, which includes unsafe paths, encrypted entries, and sizes over the limits. MarkItDown then falls back to its generic ZIP converter.
|
|
92
|
+
|
|
93
|
+
## The other direction: convert to `.mdz`
|
|
94
|
+
|
|
95
|
+
MarkItDown loses the images when it converts a Word or PowerPoint file to Markdown. The `markitdown-mdz` command keeps them: it writes an `.mdz` file holding the Markdown, with each image saved as its own file next to it.
|
|
96
|
+
|
|
97
|
+
```sh
|
|
98
|
+
pip install "markitdown-mdzip[docx,pptx]" # MarkItDown's Word and PowerPoint support
|
|
99
|
+
|
|
100
|
+
markitdown-mdz "Launch Notes.docx" # writes "Launch Notes.mdz"
|
|
101
|
+
markitdown-mdz deck.pptx -o slides.mdz
|
|
102
|
+
markitdown-mdz page.html --title "Trip Report"
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
```text
|
|
106
|
+
Launch Notes.mdz
|
|
107
|
+
├── manifest.json
|
|
108
|
+
├── Launch Notes.md ← 
|
|
109
|
+
└── images/
|
|
110
|
+
└── image-001.png
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
from markitdown_mdzip import convert_to_mdz
|
|
115
|
+
|
|
116
|
+
result = convert_to_mdz("Launch Notes.docx")
|
|
117
|
+
result.save("Launch Notes.mdz")
|
|
118
|
+
print(result.entry_point, result.title, result.images)
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
How it works:
|
|
122
|
+
- MarkItDown runs with `keep_data_uris=True`, so each image arrives embedded in the Markdown.
|
|
123
|
+
- The tool decodes each embedded image into `images/image-001.png`, `image-002.jpg`, and so on, and points the Markdown at that file. An image used twice is stored once.
|
|
124
|
+
- **The document** is named after the source file (`Launch Notes.md`). The name falls back to `index.md` when it can't be used as an archive path, or when it would be mistaken for a support file such as `README.md`.
|
|
125
|
+
- **The title** is the one MarkItDown reports. If there isn't one, it's the document's first `#` heading, then the file name.
|
|
126
|
+
|
|
127
|
+
It works for any format MarkItDown reads. Images come through for any format whose MarkItDown converter embeds them, which includes Word (`.docx`), PowerPoint (`.pptx`), and HTML pages with embedded images. MarkItDown's PDF converter extracts text only, so a PDF gives an `.mdz` without images.
|
|
128
|
+
|
|
129
|
+
| Option | CLI | Python |
|
|
130
|
+
|---|---|---|
|
|
131
|
+
| Output path | `-o out.mdz` (default: source name + `.mdz`) | `result.save(path)` |
|
|
132
|
+
| Title | `--title` | `title=` |
|
|
133
|
+
| Entry document name | — | `entry_point=` |
|
|
134
|
+
| Image folder | — | `images_dir=` (default `images`) |
|
|
135
|
+
| MarkItDown plugins | `-p` / `--use-plugins` | pass your own `markitdown=MarkItDown(...)` |
|
|
136
|
+
| Overwrite | `-f` / `--force` | — |
|
|
137
|
+
|
|
138
|
+
## Development
|
|
139
|
+
|
|
140
|
+
```sh
|
|
141
|
+
python -m venv .venv
|
|
142
|
+
.venv/Scripts/python -m pip install -e ../mdzip-core-python -e ".[docx,pptx,pdf]" python-docx
|
|
143
|
+
.venv/Scripts/python -m unittest
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Reading `.mdz` files is done by [`mdzip-core`](https://github.com/mdzip-project/mdzip-core-python), the Python MDZip library. It has no dependencies of its own.
|
|
147
|
+
|
|
148
|
+
## License
|
|
149
|
+
|
|
150
|
+
Apache-2.0
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
markitdown_mdzip/__init__.py,sha256=PAPvqPLLDhYHP6fwffusO3-dGzTHEQJA3wQA2LmjL9g,964
|
|
2
|
+
markitdown_mdzip/__main__.py,sha256=aCZiu-NNOMC76xxzCkgOPs1_gKjz85bHG8eluI4Z7nw,2156
|
|
3
|
+
markitdown_mdzip/_converter.py,sha256=-ZbD2d5v8xOFIVPGEkp26lfYh7FMRlnIH8mVHjcckDM,4766
|
|
4
|
+
markitdown_mdzip/_to_mdz.py,sha256=zRW-m0EsP6EgMzpoASxYXpW5LHzk-nyCGb3Gd6MPfWI,4971
|
|
5
|
+
markitdown_mdzip/_version.py,sha256=kUR5RAFc7HCeiqdlX36dZOHkUI5wI6V_43RpEcD8b-0,22
|
|
6
|
+
markitdown_mdzip-0.1.0.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
|
7
|
+
markitdown_mdzip-0.1.0.dist-info/METADATA,sha256=PjFI-WPWNXtVZ1P5gzsq5J_6Ce9ZLuuzZ2NlvbYV-us,6322
|
|
8
|
+
markitdown_mdzip-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
9
|
+
markitdown_mdzip-0.1.0.dist-info/entry_points.txt,sha256=GFPY2jXgehvAce_pnVl3jsil3vWurJnKYKerhh5mCHw,112
|
|
10
|
+
markitdown_mdzip-0.1.0.dist-info/top_level.txt,sha256=6zhSWfSMw0pXoxrgjUBPU3Uj--fAEDP1Zdl_9ZSRUeE,17
|
|
11
|
+
markitdown_mdzip-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
markitdown_mdzip
|