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

Support internal links in markdown long_descriptions #169

Open
Oberon00 opened this issue May 22, 2018 · 15 comments
Open

Support internal links in markdown long_descriptions #169

Oberon00 opened this issue May 22, 2018 · 15 comments

Comments

@Oberon00
Copy link

What's the problem this feature will solve?
I want to set internal links/anchors in my README.md/long_description in markdown format (for example See [the documentation](#documentation) for more information which links to a ## Documentation heading or an explicit <a name="documentation"></a> anchor). Ideally I'd like to use the same internally linked Markdown source for both GitHub and PyPI.

Describe the solution you'd like
Explicit link creation via <a name> and <a id> should be supported (maybe only <a name>, depending on the potential to disturb any Javascript code).

Ideally GitHub-compatible automatic anchor generation for headings should also be supported.

Additional context
A GitHub README.md supports two ways of setting anchors:

  • Implicitly, by generating an anchor from a heading (an <a id="lower-case-with-some-chars-replaced-by-hyphens">)
  • Explicitly, by placing <a id="..."> or <a name="..."> tags.

Warehouse does not automatically generate anchors for headings. It also strips id and name attributes from HTML embedded in the Markdown file. This leaves no possibility of linking to another location within the description (without using an absolute link to PyPI.org, which is undesirable since the README is typically used in other places like GitHub too).

@di
Copy link
Sponsor Member

di commented May 22, 2018

Thanks for the feature request!

One complication with this is that we currently use URL fragments on project pages to link to PyPI-specific things, e.g.:

If we wanted to support this, would need to make sure that this existing behavior remains.

@tomcnolan
Copy link

I was very surprised to discover this PyPI limitation, which breaks a README.md/long_description's Table of Contents. I agree with @Oberon00 that this feature is needed for consistency between Markdown source shared between GitHub and PyPI.

@mdumandag
Copy link

Any updates on this ?

@di I see that for reStructuredText documents, anchor links are working as expected. For example, see this package. https://pypi.org/project/demandlib/ . I can easily navigate to sections using the table of contents.

@di di transferred this issue from pypi/warehouse Jul 1, 2020
@di
Copy link
Sponsor Member

di commented Jul 1, 2020

I transferred this issue to pypa/readme_renderer which is responsible for rendering descriptions on PyPI.

I don't have any updates. For anyone wanting to work on this, first steps would be to determine where id/name attributes are being stripped for markdown, whether this is intentional, or can be modified, and then modifying it.

@di di added the enhancement label Jul 1, 2020
@simonw
Copy link

simonw commented Jul 23, 2020

I just ran into this problem for my package here: https://pypi.org/project/twitter-to-sqlite/

I added a table of contents to my README on https://github.com/dogsheep/twitter-to-sqlite (using this recipe) but it doesn't work on PyPI.

@simonw
Copy link

simonw commented Jul 23, 2020

It looks like Bleach is meant to be allowing the id attribute on every element here:

ALLOWED_ATTRIBUTES = {
# Bleach Defaults
"a": ["href", "title"],
"abbr": ["title"],
"acronym": ["title"],
# Custom Additions
"*": ["id"],

@simonw
Copy link

simonw commented Jul 23, 2020

... but in poking around with the library it looks like those id=... attributes aren't present even before Bleach runs. So I think the issue is in https://github.com/theacodes/cmarkgfm

@simonw
Copy link

simonw commented Jul 23, 2020

There's an open issue about that here: theacodes/cmarkgfm#20

@simonw
Copy link

simonw commented Jul 23, 2020

And I think the root issue is over in github/cmark-gfm here: github/cmark-gfm#186

simonw added a commit to simonw/til that referenced this issue Jul 23, 2020
@andrewtavis
Copy link

andrewtavis commented Mar 17, 2021

Not sure if there have been fixes in regards to all this - maybe id tags aren't being stripped anymore, or you simply need to add them explicitly - but the following works on both GitHub and PyPI now:

Contents:<a id="contents"></a> [Section Header](#section-header)

# Section Header [``](#contents) <a id="section-header"></a>

Using ↩ in single quotes creates what to me is an intuitive/appealing back button (although it's rendered differently on PyPI). See any of my projects for an example of how it looks/functions.

@vgavro
Copy link

vgavro commented Mar 26, 2021

My two cents:
For now pypi doesn't strip <a id="xxx"></a> tag, recommended way to create anchors for both github and pypi:

### Title<a id="title"></a>

(Note - no space between title and tag, otherwise you'll have self-link 🔗 pointing "title-" instead of "title")

BUT - it would be awesome if pypi would automatically create anchors same way as github do.

@Oberon00
Copy link
Author

What about the name attribute that can be used instead of id for anchors?

@Eutropios
Copy link

What's the current workaround for this?

@hugovk
Copy link
Contributor

hugovk commented Oct 17, 2023

@Eutropios #169 (comment)

@Eutropios
Copy link

@Eutropios #169 (comment)

Thank you! It'd be nice if PyPI did this automatically though 😅

github-merge-queue bot pushed a commit to databricks/databricks-sdk-py that referenced this issue Dec 5, 2023
## Changes
This PR fixes readme links so that they work in PyPI, according to this
comment: pypa/readme_renderer#169.

I've also replaced the examples section of the readme to match what we
have in our documentation, which may be easier for folks to follow.

Resolves #289.

## Tests
<!-- 
How is this tested? Please see the checklist below and also describe any
other relevant tests
-->

- [ ] `make test` run locally
- [ ] `make fmt` applied
- [ ] relevant integration tests applied
bjchambers added a commit to DewyKB/dewy that referenced this issue Feb 1, 2024
1. Images must use absolute, raw URLs so they resolve
2. The `<a name="..."></a>` doesn't work, but [this issue](
    pypa/readme_renderer#169) suggests that
    `<a id="..."></a>` may work. Giving it a try.
bjchambers added a commit to DewyKB/dewy that referenced this issue Feb 2, 2024
1. Images must use absolute, raw URLs so they resolve
2. The `<a name="..."></a>` doesn't work, but [this issue](
    pypa/readme_renderer#169) suggests that
    `<a id="..."></a>` may work. Giving it a try.
FinnWoelm added a commit to SDGClassification/benchmark that referenced this issue May 10, 2024
dhruvmanila pushed a commit to astral-sh/ruff that referenced this issue Aug 26, 2024
## Summary

This pull request adds anchor tags to the elements referenced in the
table of contents section of the readme used on
[PyPI](https://pypi.org/project/ruff/) as an attempt to fix #7257. This
update follows [this
suggestion](pypa/readme_renderer#169 (comment))
to add anchor tags (with no spaces) after the title that is to be linked
to.

## Test Plan

- This has been tested on GitHub to check that the additional tags do
not interfere with how the read me is rendered; see:
https://github.com/calumy/ruff/blob/add-links-to-pypi-docs/README.md
- MK docs were generated using the `generate_mkdocs.py` script; however
as the added tags are beyond the comment `<!-- End section: Overview
-->`, they are excluded so will not change how the docs are rendered.
- I was unable to verify how PyPI renders this change, any suggestions
would be appreciated and I can follow up on this. Hopefully, the four
thumbs up/heart on [this
comment](pypa/readme_renderer#169 (comment))
and [this
suggestion](pypa/readme_renderer#169 (comment))
all suggest that this approach should work.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants