forked from cprieto/mdx_downheader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdx_downheader.py
37 lines (28 loc) · 1.19 KB
/
mdx_downheader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from markdown import Extension
from markdown.treeprocessors import Treeprocessor
__version__ = "1.0.1"
class DownHeaderTreeProcessor(Treeprocessor):
def __init__(self, levels):
self.levels = levels
super(DownHeaderTreeProcessor, self).__init__()
def run(self, doc):
for elem in doc:
if elem.tag in ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']:
original_level = int(elem.tag[-1])
level = original_level + int(self.levels)
level = min(6, max(1, level))
elem.tag = 'h' + str(level)
class DownHeaderExtension(Extension):
def __init__(self, **kwargs):
self.config = {
'levels': [1, 'downgrade headings this many levels'],
}
super(DownHeaderExtension, self).__init__(**kwargs)
def extendMarkdown(self, md, md_globals):
if 'downheader' not in md.treeprocessors.keys():
treeprocessor = DownHeaderTreeProcessor(self.getConfig('levels'))
md.treeprocessors.add('downheader', treeprocessor, '_end')
def makeExtension(*args, **kwargs):
return DownHeaderExtension(*args, **kwargs)