Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix offset index out of bounds #7

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -4,8 +4,6 @@ on:
pull_request:
branches:
- master
paths:
- python-markdown-extension/**

jobs:
check-format:
2 changes: 2 additions & 0 deletions .github/workflows/publish-python-markdown-extension.yml
Original file line number Diff line number Diff line change
@@ -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/
4 changes: 4 additions & 0 deletions python-markdown-extension/README.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions python-markdown-extension/pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]" }]
license = { text = "Apache-2.0" }
readme = "README.md"
dependencies = [
"markdown>=3.6",
]
Original file line number Diff line number Diff line change
@@ -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))
77 changes: 77 additions & 0 deletions python-markdown-extension/test/cli.py
Original file line number Diff line number Diff line change
@@ -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)