Skip to content

add anchors to heading cells#3064

Merged
Carreau merged 4 commits intoipython:masterfrom
minrk:header-anchors
Apr 1, 2013
Merged

add anchors to heading cells#3064
Carreau merged 4 commits intoipython:masterfrom
minrk:header-anchors

Conversation

@minrk
Copy link
Copy Markdown
Member

@minrk 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 2 commits March 23, 2013 12:44
enables direct links to anchors within notebooks
@Carreau
Copy link
Copy Markdown
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
Copy link
Copy Markdown
Member Author

minrk commented Mar 23, 2013

Yes, it behaves just fine with spaces, etc.

@minrk
Copy link
Copy Markdown
Member Author

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
Copy link
Copy Markdown
Member

Carreau commented Mar 23, 2013

wouldn't the redirect remove the hashtag ?

@Carreau
Copy link
Copy Markdown
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
Copy link
Copy Markdown
Member Author

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
Copy link
Copy Markdown
Member

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

@damianavila
Copy link
Copy Markdown
Member

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
Copy link
Copy Markdown
Member Author

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 2 commits March 24, 2013 12:47
instead of the other way around.

This way, only clicking the text loads the hash link,
rather than the whole area.
@damianavila
Copy link
Copy Markdown
Member

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
Copy link
Copy Markdown
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 added a commit that referenced this pull request Apr 1, 2013
@Carreau Carreau merged commit 0e331b0 into ipython:master Apr 1, 2013
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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

@minrk minrk mentioned this pull request Apr 15, 2013
@arsenovic
Copy link
Copy Markdown

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 header-anchors branch July 29, 2013 17:03
@minrk
Copy link
Copy Markdown
Member Author

minrk commented Jul 29, 2013

In what context are you creating these links?

@arsenovic
Copy link
Copy Markdown

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

@minrk
Copy link
Copy Markdown
Member Author

minrk commented Jul 29, 2013

makes sense. Try PR #3824.

@arsenovic
Copy link
Copy Markdown

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
Copy link
Copy Markdown
Member Author

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
Copy link
Copy Markdown

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
Copy link
Copy Markdown
Member Author

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
Copy link
Copy Markdown

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
Copy link
Copy Markdown
Member Author

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
Copy link
Copy Markdown

works. thanks .

@PeterMinin
Copy link
Copy Markdown

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.

mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this pull request Nov 3, 2014
minrk added a commit to jupyter/nbconvert that referenced this pull request Apr 20, 2015
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.

5 participants