Skip to content

Preserve queries and fragments when rewriting Markdown file links - #746

Open
pabszero wants to merge 6 commits into
trentm:masterfrom
pabszero:fix-markdown-link-suffixes
Open

pabszero wants to merge 6 commits into
trentm:masterfrom
pabszero:fix-markdown-link-suffixes

Conversation

@pabszero

Copy link
Copy Markdown

With markdown-file-links, [guide](guide.md#install) keeps pointing to Markdown instead of HTML. Conversely, [part](guide.html#part.md) changes the fragment to #part.html. The same suffix check affects link definitions.

Split off query/fragment suffixes before checking the .md filename and share that conversion between inline links and definitions. Preserve the suffix exactly, including empty ?/# delimiters. Also let the extra run after definitions are stripped, so a document containing only a reference link is converted without needing another .md in the body.

Add three regression tests covering 34 inline/reference combinations, link_defs=False, and unchanged inline images. Update the README with the query/fragment behavior.

Validation on Windows, CPython 3.13.5:

  • The new tests against unchanged upstream e69bbdf produce 26 failing subtests/assertions.
  • With the fix, cd test && python test.py -- -knownfailure passes all 299 selected tests with all optional dependencies installed (including version-specific Pygments fixture selection).
  • python test/test_redos.py: all 10 cases pass.
  • git diff --check: clean. No PEP8 diagnostics on added Python lines; existing whole-file diagnostics remain (markdown2.py 412 to 410, test_markdown2.py unchanged at 68).

Other Python versions/platforms are left to CI. AI assistance: OpenAI Codex assisted with investigation, code, tests and this description. No human review or personal production usage is claimed.

@Crozzers Crozzers left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changelog also needs updating

Comment thread README.md
including an operator cell such as ```` ``|`` ````. A pipe immediately after the
closing delimiter separates the next cell.

The `markdown-file-links` extra converts `.md` file links to `.html`, preserving

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really needed here in the readme. It clogs it up and begs the question "why is this extra so important? Why not add the other 20 extras right here".
This should go in dedicated a wiki page for the extra, if one doesn't already exist

Comment thread test/test_markdown2.py

def test_markdown_file_links_query_and_fragment(self):
cases = (
("guide.md", "guide.html"),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should go in a testcase file in tests/tm-cases/, same format as the others.

Usually stuff is only put here if it's difficult to express in those test files, or so large it's impractical to commit in a file

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plus if I make this test fail an assertion on purpose I get this error, rather than anything useful. This is an issue with the test harness rather than your changes specifically, but another reason to put it in tm-cases, which doesn't have that issue

Traceback (most recent call last):
  File "/mnt/nas/Python/python-markdown2/test/test.py", line 54, in <module>
    retval = testlib.harness(testdir_from_ns=testdir_from_ns,
                             default_tags=default_tags)
  File "/mnt/nas/Python/python-markdown2/test/testlib.py", line 703, in harness
    result = test(testdir_from_ns, tags, setup_func=setup_func)
  File "/mnt/nas/Python/python-markdown2/test/testlib.py", line 396, in test
    result = runner.run(suite)
  File "/mnt/nas/Python/python-markdown2/test/testlib.py", line 539, in run
    result.printSummary()
    ~~~~~~~~~~~~~~~~~~~^^
  File "/mnt/nas/Python/python-markdown2/test/testlib.py", line 506, in printSummary
    self.printErrorList('FAIL', self.failures)
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/nas/Python/python-markdown2/test/testlib.py", line 512, in printErrorList
    % (flavour, self.getDescription(test)))
                ~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/mnt/nas/Python/python-markdown2/test/testlib.py", line 472, in getDescription
    if test._testlib_explicit_tags_:
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: '_SubTest' object has no attribute '_testlib_explicit_tags_'

Comment thread lib/markdown2.py
for key, url in self.md.urls.items():
if url.endswith('.md'):
self.md.urls[key] = url.removesuffix('.md') + '.html'
self.md.urls[key] = self._convert_url(url)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If previously self.md.urls was only mutated if the link was deemed valid (endswith('.md')), then shouldn't this do a similar check to L3949?

Saves us potentially polluting the dict with random extra URLs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants