Skip to content

Commit c4deab4

Browse files
authored
Add support for Reference Link definitions (#32)
* Addresses Github Issue #31, which is a feature request for supporting Reference Link format links.
1 parent bee614f commit c4deab4

File tree

7 files changed

+81
-4
lines changed

7 files changed

+81
-4
lines changed

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ with a variety of features:
1313
* File name linking (e.g. `[Text](file#anchor "title")`)
1414
* Absolute paths (e.g. `[Text](/link/to/file.md)`)
1515
* WikiLinks support (e.g. `[[Link#anchor|Link Title]]`)
16+
* Reference Link support (e.g. `[foo]: bar/ "Foo Title"`)
1617

1718
# Install
1819
```
@@ -30,6 +31,12 @@ plugins:
3031
3132
# Release Log
3233

34+
## Release 0.1.13
35+
Adds support for Reference Link parsing. This is to support certain Foam editors, which generate [Refeerence Links](https://spec.commonmark.org/0.29/#reference-link).
36+
37+
Issues Addressed:
38+
* GH Issue #31, `Add support for reference link definitions`. Allows compatibility with certain Foam editors which generate Reference Links.
39+
3340
## Release 0.1.12
3441
This is a bugfix release.
3542

@@ -54,6 +61,7 @@ plugins:
5461
- ezlinks:
5562
warn_ambiguities: {true|false}
5663
wikilinks: {true|false}
64+
reference_links: {true|false}
5765
```
5866
## warn_ambiguities
5967
Determines whether to warn when an abmiguous link is encountered. An ambiguous link is one that would have more than one possible targets. For example, if you had the following document setup:
@@ -95,6 +103,9 @@ Determines whether to scan for wikilinks or not (See [WikiLink Support](#wikilin
95103
> **NOTE**
96104
> This plugin feature does not function well when the 'wikilinks' markdown extension is enabled. This plugin's functionality should replace the need for enabling said extension.
97105
106+
## reference_links
107+
Determins whether to scan for Reference Links or not (See [Reference Links](https://spec.commonmark.org/0.29/#reference-link), e.g. `[foo]: /bar "Foo Bar"`)
108+
98109
# Features
99110
## Filename Links
100111
Given a layout such as

mkdocs_ezlinks_plugin/plugin.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from .replacer import EzLinksReplacer
99
from .scanners.md_link_scanner import MdLinkScanner
1010
from .scanners.wiki_link_scanner import WikiLinkScanner
11+
from .scanners.reference_link_scanner import ReferenceLinkScanner
1112
from .types import EzLinksOptions
1213

1314
LOGGER = logging.getLogger(f"mkdocs.plugins.{__name__}")
@@ -17,7 +18,8 @@
1718
class EzLinksPlugin(mkdocs.plugins.BasePlugin):
1819
config_scheme = (
1920
('wikilinks', mkdocs.config.config_options.Type(bool, default=True)),
20-
('warn_ambiguities', mkdocs.config.config_options.Type(bool, default=False))
21+
('warn_ambiguities', mkdocs.config.config_options.Type(bool, default=False)),
22+
('reference_links', mkdocs.config.config_options.Type(bool, default=False))
2123
)
2224

2325
def init(self, config):
@@ -32,6 +34,9 @@ def init(self, config):
3234
self.replacer.add_scanner(MdLinkScanner())
3335
if self.config['wikilinks']:
3436
self.replacer.add_scanner(WikiLinkScanner())
37+
38+
if self.config['reference_links']:
39+
self.replacer.add_scanner(ReferenceLinkScanner())
3540

3641
# Compile the regex once
3742
self.replacer.compile()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from typing import Pattern, Match
2+
from .base_link_scanner import BaseLinkScanner
3+
from ..types import Link
4+
5+
6+
class ReferenceLinkScanner(BaseLinkScanner):
7+
def pattern(self) -> Pattern:
8+
# +--------------------------------------+
9+
# | Reference Link Regex Capture Groups |
10+
# +--------------------------------------+
11+
# | Example: [text]: url "title" |
12+
# | |
13+
# | text: Required |
14+
# | url: Required |
15+
# | title: Optional, up to one newline |
16+
# +--------------------------------------+
17+
return r'''
18+
(?:
19+
\[
20+
(?P<ref_text>[^\]]+)
21+
\]
22+
)\:\
23+
(?!(?P<ref_protocol>[a-z][a-z0-9+\-.]*:\/\/))
24+
(?P<ref_target>\/?[^\#\ \)(\r\n|\r|\n)]*)?
25+
(?:\#(?P<ref_anchor> [^\(\ ]*)?)?
26+
(?:(\r\n|\r|\n)?)?(?P<ref_title>\ ?\"[^(\r\n|\r|\n)\"]*\")?
27+
'''
28+
29+
def match(self, match: Match) -> bool:
30+
return bool(
31+
match.groupdict().get('ref_text') and
32+
match.groupdict().get('ref_target')
33+
)
34+
35+
def extract(self, match: Match) -> bool:
36+
groups = match.groupdict()
37+
return Link(
38+
image=False,
39+
text=groups.get('ref_text') or '',
40+
target=groups.get('ref_target') or '',
41+
title=groups.get('ref_title') or '',
42+
anchor=groups.get('ref_anchor') or ''
43+
)

mkdocs_ezlinks_plugin/types.py

+1
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ class EzLinksOptions:
2727
''' Dataclass to hold typed options from the configuration. '''
2828
wikilinks: bool
2929
warn_ambiguities: bool
30+
reference_links: bool

setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
setup(
1010
name='mkdocs-ezlinks-plugin',
11-
version='0.1.12',
11+
version='0.1.13',
1212
description=description,
1313
long_description=long_description,
1414
long_description_content_type='text/markdown',
1515
keywords='mkdocs',
1616
url='https://github.com/orbikm/mkdocs-ezlinks-plugin',
17-
download_url='https://github.com/orbikm/mkdocs-ezlinks-plugin/archive/v_0.1.12.tar.gz',
17+
download_url='https://github.com/orbikm/mkdocs-ezlinks-plugin/archive/v_0.1.13.tar.gz',
1818
author='Mick Orbik',
1919
author_email='mick.orbik@gmail.com',
2020
license='MIT',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!-- Basic, proper reference link -->
2+
[Link Text]: my-section "Link Title"
3+
4+
<!-- Link with extension -->
5+
[Link Text]: my-section.md "Link Title"
6+
7+
<!-- Link without Title -->
8+
[Link Text]: my-section
9+
10+
<!-- Title with optional newline -->
11+
[Link Text]: my-section
12+
"Test Newline Title"
13+
14+
<!-- Absolute Link -->
15+
[Foo]: /other-project/project "Project"

test/mkdocs.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ nav:
1313
- 'disambiguation/sub1/sub4/index.md'
1414
- 'disambiguation/sub3/sub4/index.md'
1515
- 'disambiguation/sub3/sub4/about.md'
16+
- 'reference-links/reference-links-test.md'
1617
theme: readthedocs
1718
docs_dir: 'docs'
1819
use_directory_urls: false
1920
plugins:
2021
- search
2122
- ezlinks:
22-
wikilinks: true
23+
wikilinks: true
24+
reference_links: true

0 commit comments

Comments
 (0)