aboutsummaryrefslogtreecommitdiff
path: root/docs/extensions/HeaderId.txt
blob: efd1eb88377c8ee9aea6319c07dd61d6f9d4524b (plain)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
HeaderId
========

Summary
-------

An extension to Python-Markdown that adds an 'id' attribute to HTML header 
elements (h1-h6) in markdown's output.

This extension is included in the standard Markdown library.

Syntax
------

The basic syntax follows [PHP Markdown Extra][]'s implementation:

[PHP Markdown Extra]: http://michelf.com/projects/php-markdown/extra/#header-id

    Header 1            {#header1}
    ========

    ## Header 2 ##      {#header2}

will result in the following HTML:

    <h1 id="header1">Header 1</h1>

    <h2 id="header2">Header 2</h2>

However, there is much more that this extension does.

By default, all headers will automatically have unique "id" attributes 
generated based upon the text of the header (See below to turn this off). 
Note this example in which all three headers would have the same "id":

    #Header
    #Another Header {#header}
    #Header

Results in:

    <h1 id="header">Header</h1>
    <h1 id="header_1">Another Header</h1>
    <h1 id="header_2">Third Header</h1>

Configuring the Output
----------------------

The HeaderId extension has two configuration settings:

* **level**: Base level for headers.

    Default: `1`

* **forceid**: Force all headers to have an id.

    Default: `True`

The `level` setting allows you to automatically adjust the header levels to fit
within the hierarchy of your html templates. For example, the markdown text for
this page should not contain any headers higher than level 3 (`<h3>`). 
Therefore, do the following:

    >>>  text = '''
    ... #Some Header
    ... ## Next Level'''
    >>> html = markdown.markdown(text, ['headerid(level=3)'])
    >>> print html
    <h3 id="some_header">Some Header</h3>
    <h4 id="next_level">Next Level</h4>'

The `forceid` setting turns on or off the automatically generated ids for 
headers that do not have one explicitly defined.

    >>> text = '''
    ... # Some Header
    ... # Header with ID # { #foo }'''
    >>> html = markdown.markdown(text, ['headerid(forceid=False)'])
    >>> print html
    <h1>Some Header</h1>
    <h1 id="foo">Header with ID</h1>

Using with Meta-Data
--------------------

The HeaderId Extension also supports the [[Meta-Data]] Extension. Please see the documentation for that extension for specifics. The supported meta-data keywords are:

* `header_level`
* `header_forceid`

When used, the meta-data will override the settings provided through the  
`extension_configs` interface. 

This document:

    header_level: 2
    header_forceid: Off

    # A Header


Will result in the following output:

    <h2>A Header</h2>