Debian Codegraph @

docs/extensions/wikilinks.md

raw
1 title: WikiLinks Extension
2
3 # WikiLinks
4
5 ## Summary
6
7 The WikiLinks extension adds support for [WikiLinks][]. Specifically, any
8 ``[[bracketed]]`` word is converted to a link.
9
10 This extension is included in the standard Markdown library.
11
12 [WikiLinks]: https://en.wikipedia.org/wiki/Wikilink
13
14 !!! Note
15
16 This extension is in __maintenance mode__. We will continue to fix bugs
17 and keep it up-to-date with the core parser, but no new features or
18 changes in behavior will be made. If you need a feature that this
19 extension does not offer, then you have three options (1) find an
20 existing [third-party extension] which meets your needs, (2) [build your
21 own extension], or (3) fork this extension (pursuant to its licensing
22 requirements) and maintain it as a third-party extension.
23
24 [third-party extension]: index.md#third-party-extensions
25 [build your own extension]: api.md
26
27 ## Syntax
28
29 A ``[[bracketed]]`` word is any combination of upper or lower case letters,
30 number, dashes, underscores and spaces surrounded by double brackets. Therefore
31
32 ```md
33 [[Bracketed]]
34 ```
35
36 would produce the following HTML:
37
38 ```html
39 <a href="/Bracketed/" class="wikilink">Bracketed</a>
40 ```
41
42 Note that WikiLinks are automatically assigned `class="wikilink"` making it
43 easy to style WikiLinks differently from other links on a page if one so
44 desires. See below for ways to alter the class.
45
46 Also note that when a space is used, the space is converted to an underscore in
47 the link but left as-is in the label. Perhaps an example would illustrate this
48 best:
49
50 ```md
51 [[Wiki Link]]
52 ```
53
54 becomes
55
56 ```html
57 <a href="/Wiki_Link/" class="wikilink">Wiki Link</a>
58 ```
59
60 ## Usage
61
62 See [Extensions](index.md) for general extension usage. Use `wikilinks` as the
63 name of the extension.
64
65 See the [Library Reference](../reference.md#extensions) for information about
66 configuring extensions.
67
68 The default behavior is to point each link to the document root of the current
69 domain and close with a trailing slash. Additionally, each link is assigned to
70 the HTML class `wikilink`.
71
72 The following options are provided to change the default behavior:
73
74 * **`base_url`**: String to append to beginning of URL.
75
76 Default: `'/'`
77
78 * **`end_url`**: String to append to end of URL.
79
80 Default: `'/'`
81
82 * **`html_class`**: CSS class. Leave blank for none.
83
84 Default: `'wikilink'`
85
86 * **`build_url`**: Callable which formats the URL from its parts.
87
88 A trivial example:
89
90 ```python
91 markdown.markdown(some_text, extensions=['wikilinks'])
92 ```
93
94 ### Examples
95
96 For an example, let us suppose links should always point to the sub-directory
97 `/wiki/` and end with `.html`
98
99 ```pycon
100 >>> from markdown.extensions.wikilinks import WikiLinkExtension
101 >>> html = markdown.markdown(text,
102 ... extensions=[WikiLinkExtension(base_url='/wiki/', end_url='.html')]
103 ... )
104 ```
105
106 The above would result in the following link for `[[WikiLink]]`.
107
108 ```html
109 <a href="/wiki/WikiLink.html" class="wikilink">WikiLink</a>
110 ```
111
112 If you want to do more that just alter the base and/or end of the URL, you
113 could also pass in a callable which must accept three arguments (``label``,
114 ``base``, and ``end``). The callable must return the URL in it's entirety.
115
116 ```pycon
117 >>> def my_url_builder(label, base, end):
118 ... # do stuff
119 ... return url
120 ...
121 >>> html = markdown.markdown(text,
122 ... extensions=[WikiLinkExtension(build_url=my_url_builder)],
123 ... )
124 ```
125
126 The option is also provided to change or remove the class attribute.
127
128 ```pycon
129 >>> html = markdown.markdown(text,
130 ... extensions=[WikiLinkExtension(html_class='myclass')]
131 ... )
132 ```
133
134 Would cause all WikiLinks to be assigned to the class `myclass`.
135
136 ```html
137 <a href="/WikiLink/" class="myclass">WikiLink</a>
138 ```
139
140 ## Using with Meta-Data extension
141
142 The WikiLink extension also supports the [Meta-Data](meta_data.md) extension.
143 Please see the documentation for that extension for specifics. The supported
144 meta-data keywords are:
145
146 * `wiki_base_url`
147 * `wiki_end_url`
148 * `wiki_html_class`
149
150 When used, the meta-data will override the settings provided through the
151 `extension_configs` interface.
152
153 This document:
154
155 ```md
156 wiki_base_url: http://example.com/
157 wiki_end_url: .html
158 wiki_html_class:
159
160 A [[WikiLink]] in the first paragraph.
161 ```
162
163 would result in the following output (notice the blank `wiki_html_class`):
164
165 ```html
166 <p>A <a href="http://example.com/WikiLink.html">WikiLink</a> in the first paragraph.</p>
167 ```