Skip to content
This repository has been archived by the owner on May 17, 2018. It is now read-only.

Commit

Permalink
Merge pull request #211 from facelessuser/master
Browse files Browse the repository at this point in the history
Github style header anchors
  • Loading branch information
Julien Bouquillon committed Jul 4, 2014
2 parents 718c02d + 06fdb65 commit 4224359
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 4 deletions.
53 changes: 49 additions & 4 deletions markdown.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
@font-face{
/*
octicons font
https://octicons.github.com/
But only character f05c (link icons) to keep size down
*/
font-family: octicons-anchor;
src: url(data:x-font-woff;charset=utf-8;base64,d09GRgABAAAAAAYcAA0AAAAACjQAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAABGRlRNAAABMAAAABwAAAAca8vGTk9TLzIAAAFMAAAARAAAAFZG1VHVY21hcAAAAZAAAAA+AAABQgAP9AdjdnQgAAAB0AAAAAQAAAAEACICiGdhc3AAAAHUAAAACAAAAAj//wADZ2x5ZgAAAdwAAADRAAABEKyikaNoZWFkAAACsAAAAC0AAAA2AtXoA2hoZWEAAALgAAAAHAAAACQHngNFaG10eAAAAvwAAAAQAAAAEAwAACJsb2NhAAADDAAAAAoAAAAKALIAVG1heHAAAAMYAAAAHwAAACABEAB2bmFtZQAAAzgAAALBAAAFu3I9x/Nwb3N0AAAF/AAAAB0AAAAvaoFvbwAAAAEAAAAAzBdyYwAAAADP2IQvAAAAAM/bz7t4nGNgZGFgnMDAysDB1Ml0hoGBoR9CM75mMGLkYGBgYmBlZsAKAtJcUxgcPsR8iGF2+O/AEMPsznAYKMwIkgMA5REMOXicY2BgYGaAYBkGRgYQsAHyGMF8FgYFIM0ChED+h5j//yEk/3KoSgZGNgYYk4GRCUgwMaACRoZhDwCs7QgGAAAAIgKIAAAAAf//AAJ4nHWMMQrCQBBF/0zWrCCIKUQsTDCL2EXMohYGSSmorScInsRGL2DOYJe0Ntp7BK+gJ1BxF1stZvjz/v8DRghQzEc4kIgKwiAppcA9LtzKLSkdNhKFY3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ADC0U/jLcBZDKrMhUBfQBvXRzLtFtjU9eNHKin0x5InTqb8lNpfKv1s1xHzTXRqgKzek/mb7nB8RZTCDhGEX3kK/8Q75AmUM/eLkfA+0Hi908Kx4eNsMgudg5GLdRD7a84npi+YxNr5i5KIbW5izXas7cHXIMAau1OueZhfj+cOcP3P8MNIWLyYOBuxL6DRylJ4cAAAB4nGNgYoAALjDJyIAOWMCiTIxMLDmZedkABtIBygAAAA==) format('woff');
}

html { font-size: 100%; overflow-y: scroll; -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; }

body{
Expand All @@ -9,7 +19,7 @@ body{
background:#fefefe;
width: 45em;
margin: 10px auto;
padding: 1em;
padding: 30px;
outline: 1300px solid #FAFAFA;
}

Expand All @@ -20,6 +30,40 @@ a:active{ color:#faa700; }
a:focus{ outline: thin dotted; }
a:hover, a:active{ outline: 0; }

a.headeranchor-link{
color:#111;
border: 0;
margin-left: -20px;
padding-right: 6px;
cursor: pointer;
position: absolute;
display: block;
top: 0;
left: 0;
bottom: 0;
}

span.headeranchor {
font: normal normal 16px octicons-anchor;
line-height: 1;
-moz-osx-font-smoothing: grayscale;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}

a.headeranchor-link:hover { text-decoration: none;}
a.headeranchor-link:visited { color:#4d4d4c; }
a.headeranchor-link:active { color:#4d4d4c; }
a.headeranchor-link:hover span.headeranchor:before { content: '\f05c'; }
h1:hover a span.headeranchor:before { content: '\f05c'; }
h2:hover a span.headeranchor:before { content: '\f05c'; }
h3:hover a span.headeranchor:before { content: '\f05c'; }
h4:hover a span.headeranchor:before { content: '\f05c'; }
h5:hover a span.headeranchor:before { content: '\f05c'; }
h6:hover a span.headeranchor:before { content: '\f05c'; }

span.backtick {
border:1px solid #EAEAEA;
border-radius:3px;
Expand All @@ -42,9 +86,10 @@ max-width:100%;
}

h1,h2,h3,h4,h5,h6{
font-weight:normal;
color:#111;
line-height:1em;
font-weight:normal;
color:#111;
line-height:1em;
position: relative;
}
h4,h5,h6{ font-weight: bold; }
h1{ font-size:2.5em; }
Expand Down
68 changes: 68 additions & 0 deletions markdown/extensions/headeranchor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""
mdownx.headeranchor
An extension for Python Markdown.
Github header anchors
MIT license.
Copyright (c) 2014 Isaac Muse <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
from __future__ import unicode_literals
from __future__ import absolute_import
from ..extensions import Extension
from ..treeprocessors import Treeprocessor
from .headerid import slugify, stashedHTML2text, itertext

LINK = '<a name="user-content-%(id)s" href="#%(id)s" class="headeranchor-link" aria-hidden="true"><span class="headeranchor"></span></a>'


class HeaderAnchorTreeprocessor(Treeprocessor):
def run(self, root):
""" Add header anchors """

for tag in root.getiterator():
if tag.tag in ('h1', 'h2', 'h3', 'h4', 'h5', 'h6'):
if "id" in tag.attrib:
id = tag.get('id')
else:
id = stashedHTML2text(''.join(itertext(tag)), self.md)
id = slugify(id, self.config.get('sep'))
tag.set('id', id)
tag.text = self.markdown.htmlStash.store(
LINK % {"id": id},
safe=True
) + tag.text
return root


class HeaderAnchorExtension(Extension):
def __init__(self, configs):
self.config = {
'sep': ['-', "Separator to use when creating header ids - Default: '-'"]
}

for key, value in configs:
self.setConfig(key, value)

def extendMarkdown(self, md, md_globals):
"""Add HeaderAnchorTreeprocessor to Markdown instance"""

self.processor = HeaderAnchorTreeprocessor(md)
self.processor.config = self.getConfigs()
self.processor.md = md
if 'toc' in md.treeprocessors.keys():
insertion = ">toc"
else:
insertion = ">_end"
md.treeprocessors.add("headeranchor", self.processor, insertion)
md.registerExtension(self)


def makeExtension(configs={}):
return HeaderAnchorExtension(configs=configs)

0 comments on commit 4224359

Please sign in to comment.