add anchors to heading cells #3064

Merged
merged 4 commits into from Apr 1, 2013

Projects

None yet

5 participants

@minrk
Member
minrk commented Mar 23, 2013

and trigger standard anchor scrolling on notebook load. It doesn't work regular on page load because the elements don't exist yet.

minrk added some commits Mar 23, 2013
@Carreau
Member
Carreau commented Mar 23, 2013

Does it behaves correctly when text have space/special char ? Do you think we should reduce the used caracter for the anchor to [a-zA-Z0-9_-] ?

@minrk
Member
minrk commented Mar 23, 2013

Yes, it behaves just fine with spaces, etc.

@minrk
Member
minrk commented Mar 23, 2013

I should note that this plus #3058 mean you can have links to particular anchors inside other notebooks, with syntax like:

[other notebook summary](Other Notebook.ipynb#Summary)
@Carreau
Member
Carreau commented Mar 23, 2013

wouldn't the redirect remove the hashtag ?

@Carreau
Member
Carreau commented Mar 23, 2013

And the notebook regex _notebook_name_regex = r"(?P<notebook_name>.+\.ipynb)" will probably not match if there is a hashtag also.

@minrk
Member
minrk commented Mar 23, 2013

wouldn't the redirect remove the hashtag ?
And the notebook regex _notebook_name_regex = r"(?P<notebook_name>.+.ipynb)" will probably not match if there is a hashtag also.

No and no. The hash is not actually part of the URL - it is part of the request, much like url parameters after ?.

I have tested locally with both PRs merged, and confirmed that it works as expected.

@damianavila
Contributor

This one with #3058 is really, really great! I hope to see both merged soon ;-)

@damianavila
Contributor

One question? Wouldn't be beneficial to have anchors not only to heading cells, but also to any other cell in the notebook? It would be relevant when you have a lot of code in multiple cells an you want to point out to a specific code cell (and do not want to add a heading just for tagging)...

@minrk
Member
minrk commented Mar 24, 2013

Yes, I think that will make sense. It wasn't obvious to me what those tags should be, so I left it for another time. One option would be to use the cell ID metadata, but I'm not convinced yet.

minrk added some commits Mar 24, 2013
@minrk minrk put the anchor inside the header tag
instead of the other way around.

This way, only clicking the text loads the hash link,
rather than the whole area.
32fef00
@minrk minrk s/heading-link/heading-anchor/ 7067d4a
@damianavila
Contributor

I was also thinking in the cell metadata... This is an infrastructure available right now and we can use it to get anchors to each cell.

@Carreau
Member
Carreau commented Mar 27, 2013

IMHO, cell id is delicate, it should be baked into the notebook format seeing the number of things that could rely on it. Otherwise I'm ok with the hash.

+1 for merging.

@Carreau Carreau merged commit 0e331b0 into ipython:master Apr 1, 2013

1 check passed

default The Travis build passed
Details
@Carreau Carreau commented on the diff Apr 7, 2013
IPython/frontend/html/notebook/static/css/style.min.css
@@ -934,6 +934,7 @@ pre,code,kbd,samp{white-space:pre-wrap;}
.js-error{color:darkred;}
a{text-decoration:underline;}
p{margin-bottom:0;}
+a.heading-anchor:link{text-decoration:none;color:inherit;}
@Carreau
Carreau Apr 7, 2013 Member

was the pseudo selector :link intended ? Or did you ment to apply the style to even the non-visited link ?

@minrk
minrk Apr 7, 2013 Member

I believe this code is exactly what I meant it to be, but I'm still shaky on CSS. It prevents adding link-styling to the heading text.

@Carreau
Carreau Apr 7, 2013 Member

it seem to me that link is only a subcategory on link. Deployed on nbviewer it does not remove the styling...but it might be a combine effect with the fact that the anchor tag do not have href attributes.

@minrk
minrk Apr 7, 2013 Member

This definitely does the right thing on the live notebook, so nbconvert must not be generating the same HTML.

@Carreau
Carreau Apr 7, 2013 Member

For me, once the link/header are clicked once, they appear blue.

@Carreau
Carreau Apr 7, 2013 Member

The only way I found to have it correctly on live notebook is to set both :link and :visited one with color:inherit the other without.

@minrk
minrk Apr 7, 2013 Member

My custom CSS got in the way, PR #3143 should fix. Thanks for the vigilance!

@minrk
minrk Apr 7, 2013 Member

Both should have color:inherit - the heading text should never be colored.

@minrk minrk referenced this pull request Apr 15, 2013
Closed

links across notebooks #3056

@arsenovic

When i link to a heading within a single notebook, an entirely new notebook session is opened (in a new tab).
is it possible to prevent this and just scroll to the heading in the original notebook?

@minrk minrk deleted the minrk:header-anchors branch Jul 29, 2013
@minrk
Member
minrk commented Jul 29, 2013

In what context are you creating these links?

@arsenovic

in a markdown cell i have [header link](#header1), where header1 is a H1 cell in the same notebook.

@minrk
Member
minrk commented Jul 29, 2013

makes sense. Try PR #3824.

@arsenovic

In a comment above it is said that these links work for notebooks that have spaces in their names. This does not appear to work for me.

in a markdown cell i have:
here is an external link , [name with spaces](name with spaces.ipynb)

once executed, the result is not highlighted. i have also noticed that linking to local headings (#3824) that contain spaces requires the spaces to be replaced with hyphens '-'

@minrk
Member
minrk commented Jul 30, 2013

Yes - I was wrong above, and the hyphen fix was made in a later PR (I don't recall which).

@arsenovic

ok thanks. So, to be clear:

  • internal links work with a spaces changed to hyphens, but
  • external links dont work with spaces changed to hyphens.
@minrk
Member
minrk commented Jul 30, 2013

Both internal and external links work by substituting hyphen for space. The id of the div has only one value, so they mustn't be different. The following links:

[external](A.ipynb#has-space)
[internal](#has-space)

both work for linking to a heading cell with the text "has space" (just tested and confirmed with master).

@arsenovic

yes, what i was trying to describe was a link to a notebook, which had spaces in its name.

[external](has space too.ipynb#has-space)

@minrk
Member
minrk commented Jul 30, 2013

Ah, gotcha. Yes, there is a difference between a URL path and an anchor ID. It looks like there is a bug in the markdown parser for URLs. If you put %20 in place of spaces for the actual page URL (normally browsers do this substitution behind the scenes), it should work.

@arsenovic

works. thanks .

@PeterMinin

Hi, thanks for this feature, but here's a use case that isn't currently supported. If I have headers like this:

  • Setup 1
    • Unnormalized
    • Normalized
  • Setup 2
    • Unnormalized
    • Normalized

i.e. some headers are duplicated under different parents, the anchors of all "copies" point to the first one. Do you think this use case is worth supporting or should I just work around it? I think it would be convenient to have such a feature. It could be implemented by prefixing anchors with all higher level headers' anchors.

@minrk minrk added a commit to jupyter/nbconvert that referenced this pull request Apr 20, 2015
@minrk minrk add heading-anchor b206506
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment