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

Automatic anchor links #344

Closed
buremba opened this issue Feb 17, 2017 · 4 comments
Closed

Automatic anchor links #344

buremba opened this issue Feb 17, 2017 · 4 comments
Assignees

Comments

@buremba
Copy link

buremba commented Feb 17, 2017

Is there any way to add anchor links with showdown similar to Github READMEs?
ylhyu1hikk

@tivie
Copy link
Member

tivie commented Feb 18, 2017

Not out of the box. But it's quite possible with an extension (check fiddle https://jsfiddle.net/tivie/knf9nb8f/)

// Extension
showdown.extension('header-anchors', function() {

	var ancTpl = '$1<a id="user-content-$3" class="anchor" href="#$3" aria-hidden="true"><svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>$4';

  return [{
    type: 'html',
    regex: /(<h([1-3]) id="([^"]+?)">)(.*<\/h\2>)/g,
    replace: ancTpl
  }];
});

@tivie tivie self-assigned this Feb 18, 2017
@buremba
Copy link
Author

buremba commented Feb 18, 2017

@tivie thanks a lot!

@buremba buremba closed this as completed Feb 18, 2017
@kirkins
Copy link

kirkins commented Mar 12, 2017

Is this for anchor links that have a space?
I'm finding that all my single word anchors work automatically but not anchors with two or more words.

@jiasli
Copy link

jiasli commented Mar 7, 2019

A prettier css with unnecessary space removed. Borrowed from GitHub css.

.anchor
{
    float: left;
    line-height: 1;
    margin-left: -20px;
    padding-right: 4px
}

h1 .anchor:focus,
h2 .anchor:focus,
h3 .anchor:focus
{
  outline: none
}

h1 .octicon-link,
h2 .octicon-link,
h3 .octicon-link
{
  color: #1b1f23;
  vertical-align: middle;
  visibility: hidden
}

h1:hover .anchor .octicon-link,
h2:hover .anchor .octicon-link,
h3:hover .anchor .octicon-link
{
  visibility: visible
}

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

4 participants