Skip to content

Commit d1121bb

Browse files
author
Zach Hannum
committedJan 14, 2020
Initial commit
0 parents  commit d1121bb

File tree

8 files changed

+185
-0
lines changed

8 files changed

+185
-0
lines changed
 

‎LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2018 Zach Hannum
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

‎README.md

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# MkDocs Autolinks Plugin
2+
3+
* * *
4+
5+
*An MkDocs plugin that simplifies relative linking between documents.*
6+
7+
The Autolinks plugins allows you to link to pages and images within your MkDocs site without provided the entire relative path to the file in your document structure.
8+
9+
## Installation
10+
11+
* * *
12+
13+
Install the package with pip:
14+
15+
```sh
16+
cd mkdocs-autolinks-plugin
17+
pip install .
18+
```
19+
20+
Enable the plugin in your `mkdocs.yml`:
21+
22+
```yaml
23+
plugins:
24+
- autolinks
25+
```
26+
27+
More information about plugins in the [MkDocs documentation](https://www.mkdocs.org/user-guide/plugins/).
28+
29+
## Usage
30+
31+
* * *
32+
33+
To use this plugin, simply create a link that only contains the filename of file you wish to link to.
34+
35+
For example, say you have a document structure like this:
36+
37+
```
38+
docs/
39+
├── guides/
40+
│ ├── onboarding.md
41+
│ └── syntax_guide.md
42+
├── software/
43+
│ ├── git_flow.md
44+
│ └── qnx.md
45+
└── images/
46+
├── avatar.png
47+
└── example.jpg
48+
```
49+
50+
Normally, if you want create a link to `git_flow.md` from inside `onboarding.md`, you would need to provide the relative path:
51+
52+
```markdown
53+
# onboarding.md
54+
[Git Flow](../software/git_flow.md)
55+
```
56+
57+
This link is fragile; if someone decides to rearrange the site structure, all of these relative links break. Not to mention having to figure out the relative path.
58+
59+
With the Autolinks plugin, you simply need to provide the filename you wish to link to. The plugin will pre-process all of your markdown files and replace the filename with the correct relative path, given that the file exists in your document structure:
60+
61+
```markdown
62+
# onboarding.md
63+
[Git Flow](git_flow.md)
64+
```
65+
66+
The Autolinks plugin also works with `jpg` and `png` files:
67+
68+
```markdown
69+
# onboarding.md
70+
![Avatar](avatar.png)
71+
```

‎deploy.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
rm -rf dist
4+
python setup.py bdist_wheel sdist --formats gztar && twine upload dist/*

‎mkdocs_autolinks_plugin/__init__.py

Whitespace-only changes.

‎mkdocs_autolinks_plugin/plugin.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import re
2+
import os
3+
from mkdocs.plugins import BasePlugin
4+
5+
AUTOLINK_RE = r'\[([^\]]+)\]\(([^)/]+\.(md|png|jpg))\)'
6+
# (?<!```\n)\[([^\]]+)\]\(([^)/]+\.md)\)
7+
class AutoLinkReplacer:
8+
def __init__(self, base_docs_url, page_url):
9+
self.base_docs_url = base_docs_url
10+
self.page_url = page_url
11+
12+
def __call__(self, match):
13+
# Name of the markdown file
14+
filename = match.group(2).strip()
15+
16+
# Absolute URL of the linker
17+
abs_linker_url = os.path.dirname(os.path.join(self.base_docs_url, self.page_url))
18+
19+
# Find directory URL to target link
20+
rel_link_url = ''
21+
# Walk through all files in docs directory to find a matching file
22+
for root, dirs, files in os.walk(self.base_docs_url):
23+
for name in files:
24+
# If we have a match, create the relative path from linker to the link
25+
if name == filename:
26+
# Absolute path to the file we want to link to
27+
abs_link_url = os.path.dirname(os.path.join(root, name))
28+
# Constructing relative path from the linker to the link
29+
rel_link_url = os.path.join(os.path.relpath(abs_link_url, abs_linker_url), filename)
30+
if rel_link_url == '':
31+
print('WARNING: AutoLinksPlugin unable to find ' + filename + ' in directory ' + self.base_docs_url)
32+
return match.group(0)
33+
34+
# Construct the return link by replacing the filename with the relative path to the file
35+
link = match.group(0).replace(match.group(2), rel_link_url)
36+
37+
return link
38+
39+
class AutoLinksPlugin(BasePlugin):
40+
41+
def on_page_markdown(self, markdown, page, config, site_navigation=None, **kwargs):
42+
43+
# Getting the root location of markdown source files
44+
base_docs_url = config["docs_dir"]
45+
46+
# Getting the page url that we are linking from
47+
page_url = page.file.src_path
48+
49+
# Look for matches and replace
50+
markdown = re.sub(AUTOLINK_RE, AutoLinkReplacer(base_docs_url, page_url), markdown)
51+
52+
return markdown

‎requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mkdocs==1.0.3

‎setup.cfg

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[metadata]
2+
description-file = README.md

‎setup.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from setuptools import setup, find_packages
2+
3+
setup(
4+
name='mkdocs-autolinks-plugin',
5+
version='0.1.0',
6+
description='An MkDocs plugin',
7+
long_description='An MkDocs plugin that automagically generates relative links between markdown pages',
8+
keywords='mkdocs',
9+
url='https://github.com/midnightprioriem/mkdocs-autolinks-plugin',
10+
download_url='https://github.com/midnightprioriem/mkdocs-autolinks-plugin/archive/v_010.tar.gz',
11+
author='Zach Hannum',
12+
author_email='zacharyhannum@gmail.com',
13+
license='MIT',
14+
python_requires='>=2.7',
15+
install_requires=[
16+
'mkdocs>=1.0.4',
17+
],
18+
classifiers=[
19+
'Development Status :: 4 - Beta',
20+
'Intended Audience :: Developers',
21+
'Intended Audience :: Information Technology',
22+
'License :: OSI Approved :: MIT License',
23+
'Programming Language :: Python',
24+
'Programming Language :: Python :: 3 :: Only',
25+
'Programming Language :: Python :: 3.4',
26+
'Programming Language :: Python :: 3.5',
27+
'Programming Language :: Python :: 3.6',
28+
'Programming Language :: Python :: 3.7'
29+
],
30+
packages=find_packages(),
31+
entry_points={
32+
'mkdocs.plugins': [
33+
'autolinks = mkdocs_autolinks_plugin.plugin:AutoLinksPlugin',
34+
]
35+
}
36+
)

0 commit comments

Comments
 (0)
Please sign in to comment.