MyST-NB
MyST-NB copied to clipboard
Create unique url's / anchors for each code cell
Is your feature request related to a problem? Please describe.
Very often, once I have created a resource on JB, I find myself wanting to link to specific code cells to illustrate a solution for questions from students, for example.
Describe the solution you'd like
I know it is possible to create unique links with the (<id>)= pattern but this'd require to manually hardcode it on to every code cell just in case/for when some of them will be required.
Ideally, in a similar way to how there is a button that appears on the cell to copy the code, there'd be another one that provides a unique and direct URL to that code cell (it could perhaps be related to the cell number/position in the underlying notebook?).
Describe alternatives you've considered
Currently I default to link to the closest section on which that cell belongs to, and mostly works; or create manual links if I know I'll need them.
This is a cool idea - I'm not sure where the best place would be to implement this, since we'd be bottlenecked on how Sphinx itself handles cross-refs (I think that you must put a label just before a header title, for example). But would be cool to experiment with and implement.
Oh its already in my thinking for myst-nb: I've already implemented a similar thing previously for ipypublish: https://ipypublish.readthedocs.io/en/latest/code_cells.html#plots-with-matplotlib
Nice - shall we move this to myst-nb then?
Yep sounds good 👍
Thanks for opening your first issue here! Engagement like this is essential for open source projects! :hugs:
If you haven't done so already, check out EBP's Code of Conduct. Also, please try to follow the issue template as it helps other community members to contribute more effectively.
If your issue is a feature request, others may react to it, to raise its prominence (see Feature Voting).
Welcome to the EBP community! :tada:
Anchors for cell based on cell IDs have been implemented in JupyterLab (https://github.com/jupyterlab/jupyterlab/pull/13285) and nbconvert (https://github.com/jupyter/nbconvert/pull/1897). There is also a related issue open on Jupyter Book repo https://github.com/executablebooks/jupyter-book/issues/1812.
Could the myst_parser expose the _heading_slugs logic from https://github.com/executablebooks/MyST-Parser/pull/718 so that we could add anchors to cells reusing the same reference collection logic?
Edit: I mean having a function similar to existing generate_heading_target, but allowing to set an arbitrary slug (and use it on cell nodes).
@chrisjsewell would you agree that the above proposal of exposing _heading_slugs logic is going in the right direction? I can open a PR against MyST-parser to complement the preliminary work in https://github.com/executablebooks/MyST-NB/pull/519 but would appreciate an early feedback.
https://github.com/executablebooks/MyST-NB/pull/519 is ready for review; I will need help over there - despite many attempts the slugs do not get embedded as identifiers in the cells. It seems like the target processing code does not get invoked but I cannot narrow it down why.