diff --git a/.github/workflows/check-format-and-test-python-markdown-extension.yml b/.github/workflows/check-format-and-test-python-markdown-extension.yml index df6737de..d7bf5014 100644 --- a/.github/workflows/check-format-and-test-python-markdown-extension.yml +++ b/.github/workflows/check-format-and-test-python-markdown-extension.yml @@ -4,8 +4,6 @@ on: pull_request: branches: - master - paths: - - python-markdown-extension/** jobs: check-format: diff --git a/.github/workflows/publish-python-markdown-extension.yml b/.github/workflows/publish-python-markdown-extension.yml index adce9bda..36f328b9 100644 --- a/.github/workflows/publish-python-markdown-extension.yml +++ b/.github/workflows/publish-python-markdown-extension.yml @@ -31,3 +31,5 @@ jobs: name: Build Package - uses: pypa/gh-action-pypi-publish@release/v1 name: Publish package distributions to PyPI + with: + packages-dir: python-markdown-extension/dist/ diff --git a/python-markdown-extension/README.md b/python-markdown-extension/README.md new file mode 100644 index 00000000..e8caa675 --- /dev/null +++ b/python-markdown-extension/README.md @@ -0,0 +1,4 @@ +# python_markdown_document_offsets_injection_extension + +A Python-Markdown extension to count and calculate offset between rendered html and origin markdown document, +inject them into the html elements. diff --git a/python-markdown-extension/pyproject.toml b/python-markdown-extension/pyproject.toml index b641660a..c4af3eef 100644 --- a/python-markdown-extension/pyproject.toml +++ b/python-markdown-extension/pyproject.toml @@ -4,6 +4,7 @@ version = "0.0.1" description = "A Python-Markdown compiler plugin that put markdown words offset to the output HTML." authors = [{ name = "HikariLan", email = "hikarilan@minecraft.kim" }] license = { text = "Apache-2.0" } +readme = "README.md" dependencies = [ "markdown>=3.6", ] diff --git a/python-markdown-extension/src/python_markdown_document_offsets_injection_extension/extension.py b/python-markdown-extension/src/python_markdown_document_offsets_injection_extension/extension.py index a2aa3b5b..1d6152a7 100644 --- a/python-markdown-extension/src/python_markdown_document_offsets_injection_extension/extension.py +++ b/python-markdown-extension/src/python_markdown_document_offsets_injection_extension/extension.py @@ -236,6 +236,9 @@ def run(self, parent: etree.Element, blocks: list[str]) -> bool: # append MARK_PREVENT_RECURSION to tail of the block to prevent recursion, we don't use a handled # flaglist because we don't know if there's some same block in the document self.parser.parseBlocks(parent, [block + MARK_PREVENT_RECURSION]) + # fix offset index out of range + if len(parent) == 0: + return True # fix multi blocks in same parents if self.meta["last_parent"] == parent[-1]: parent[-1].set("data-original-document-end", str(end)) diff --git a/python-markdown-extension/test/cli.py b/python-markdown-extension/test/cli.py new file mode 100644 index 00000000..f1fc1643 --- /dev/null +++ b/python-markdown-extension/test/cli.py @@ -0,0 +1,77 @@ +import argparse +from argparse import FileType +import markdown + +from pymdownx.emoji import to_svg +from pymdownx.slugs import uslugify +from pymdownx.arithmatex import fence_mathjax_format + +parser = argparse.ArgumentParser("cli") +parser.add_argument( + "file", help="Markdown file to be rendered", type=FileType(encoding="utf-8") +) +args = parser.parse_args() + +result = markdown.markdown( + args.file.read(), + extensions=[ + "document-offsets-injection", + "admonition", + "def_list", + "footnotes", + "meta", + "toc", + "pymdownx.arithmatex", + "pymdownx.caret", + "pymdownx.critic", + "pymdownx.details", + "pymdownx.emoji", + "pymdownx.highlight", + "pymdownx.inlinehilite", + "pymdownx.keys", + "pymdownx.magiclink", + "pymdownx.mark", + "pymdownx.snippets", + "pymdownx.progressbar", + "pymdownx.smartsymbols", + "pymdownx.superfences", + "pymdownx.tasklist", + "pymdownx.tilde", + "pymdownx.tabbed", + ], + extension_configs={ + "toc": { + "permalink": "", + "slugify": uslugify, + }, + "pymdownx.arithmatex": { + "generic": True, + }, + "pymdownx.emoji": { + "emoji_generator": to_svg, + }, + "pymdownx.highlight": { + "linenums": True, + }, + "pymdownx.snippets": { + "check_paths": True, + }, + "pymdownx.superfences": { + "custom_fences": [ + { + "name": "math", + "class": "arithmatex", + "format": fence_mathjax_format, + }, + ], + }, + "pymdownx.tasklist": { + "custom_checkbox": True, + }, + "pymdownx.tabbed": { + "alternate_style": True, + }, + }, +) + +print(result)