Switched to markdown-header-anchors from pypi
This commit is contained in:
parent
b0d0a62c3b
commit
f972cb9179
|
@ -12,3 +12,4 @@ default = yes
|
||||||
[packages]
|
[packages]
|
||||||
lektor-webpack-support = 0.1
|
lektor-webpack-support = 0.1
|
||||||
lektor-disqus-comments = 0.1
|
lektor-disqus-comments = 0.1
|
||||||
|
lektor-markdown-header-anchors = 0.1
|
||||||
|
|
|
@ -1,42 +0,0 @@
|
||||||
from lektor.pluginsystem import Plugin
|
|
||||||
from lektor.utils import slugify
|
|
||||||
from markupsafe import Markup
|
|
||||||
from collections import namedtuple
|
|
||||||
|
|
||||||
|
|
||||||
TocEntry = namedtuple('TocEntry', ['anchor', 'title', 'children'])
|
|
||||||
|
|
||||||
|
|
||||||
class MarkdownHeaderAnchorsPlugin(Plugin):
|
|
||||||
name = 'Markdown Header Anchors'
|
|
||||||
description = 'Adds anchors to markdown headers.'
|
|
||||||
|
|
||||||
def on_markdown_config(self, config, **extra):
|
|
||||||
class HeaderAnchorMixin(object):
|
|
||||||
def header(renderer, text, level, raw):
|
|
||||||
anchor = slugify(raw)
|
|
||||||
renderer.meta['toc'].append((level, anchor, Markup(text)))
|
|
||||||
return '<h%d id="%s">%s</h%d>' % (level, anchor, text, level)
|
|
||||||
config.renderer_mixins.append(HeaderAnchorMixin)
|
|
||||||
|
|
||||||
def on_markdown_meta_init(self, meta, **extra):
|
|
||||||
meta['toc'] = []
|
|
||||||
|
|
||||||
def on_markdown_meta_postprocess(self, meta, **extra):
|
|
||||||
prev_level = None
|
|
||||||
toc = []
|
|
||||||
stack = [toc]
|
|
||||||
|
|
||||||
for level, anchor, title in meta['toc']:
|
|
||||||
if prev_level is None:
|
|
||||||
prev_level = level
|
|
||||||
elif prev_level == level - 1:
|
|
||||||
stack.append(stack[-1][-1][2])
|
|
||||||
prev_level = level
|
|
||||||
elif prev_level > level:
|
|
||||||
while prev_level > level:
|
|
||||||
stack.pop()
|
|
||||||
prev_level -= 1
|
|
||||||
stack[-1].append(TocEntry(anchor, title, []))
|
|
||||||
|
|
||||||
meta['toc'] = toc
|
|
|
@ -1,12 +0,0 @@
|
||||||
from setuptools import setup
|
|
||||||
|
|
||||||
setup(
|
|
||||||
name='lektor-markdown-header-anchors',
|
|
||||||
version='0.1',
|
|
||||||
py_modules=['lektor_markdown_header-anchors'],
|
|
||||||
entry_points={
|
|
||||||
'lektor.plugins': [
|
|
||||||
'markdown-header-anchors = lektor_markdown_header_anchors:MarkdownHeaderAnchorsPlugin',
|
|
||||||
]
|
|
||||||
}
|
|
||||||
)
|
|
Loading…
Reference in New Issue