-
Notifications
You must be signed in to change notification settings - Fork 79
✨ NEW: Add linkify #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
✨ NEW: Add linkify #78
Conversation
Codecov Report
@@ Coverage Diff @@
## master #78 +/- ##
==========================================
+ Coverage 95.64% 95.67% +0.02%
==========================================
Files 76 77 +1
Lines 3813 3907 +94
==========================================
+ Hits 3647 3738 +91
- Misses 166 169 +3
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
The final piece of the puzzle! |
Is this implementation good? If it's OK, increase coverage |
Well it passes all the markdown-it fixture tests. So that's always a good start 😀 @hukkinj1 might also want to have a look? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! I would suggest a change in how the "linkify enabled but not installed" case is handled, that should result in overall a bit less code, and avoids giving LinkifyIt.pretest
a secondary task.
markdown_it/extra/__init__.py
Outdated
@@ -0,0 +1 @@ | |||
from .linkify_it import LinkifyIt # noqa: F401 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from .linkify_it import LinkifyIt # noqa: F401 |
markdown_it/extra/linkify_it.py
Outdated
class NotImportedError(Exception): | ||
"""Not imported ``linkify-it-py`` package error""" | ||
|
||
def __init__(self): | ||
message = ( | ||
"If you want to use the 'linkify', " | ||
+ "you must pre-install the 'linkify-it-py' package. " | ||
+ "Please try 'pip install linkify-it-py'." | ||
) | ||
super().__init__(message) | ||
|
||
|
||
class LinkifyIt: | ||
"""linkify-it-py dummy class. | ||
|
||
If ``linkify-it-py`` is not installed, this dummy class will be called. And | ||
raise :class:`markdown_it.extra.linkify_it.NotImportedError` when input data | ||
is parsed via core.rules (linkify). | ||
""" | ||
|
||
def __init__(self, schemas=None, options=None): | ||
pass | ||
|
||
# def add(self, schema, definition): | ||
# raise NotImportedError | ||
|
||
# def match(self, text): | ||
# raise NotImportedError | ||
|
||
def pretest(self, text): | ||
raise NotImportedError | ||
|
||
# def set(self, options): | ||
# raise NotImportedError | ||
|
||
# def test(self, text): | ||
# raise NotImportedError | ||
|
||
# def test_schema_at(self, text, name, position): | ||
# raise NotImportedError | ||
|
||
# def tlds(self, list_tlds, keep_old=False): | ||
# raise NotImportedError |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class NotImportedError(Exception): | |
"""Not imported ``linkify-it-py`` package error""" | |
def __init__(self): | |
message = ( | |
"If you want to use the 'linkify', " | |
+ "you must pre-install the 'linkify-it-py' package. " | |
+ "Please try 'pip install linkify-it-py'." | |
) | |
super().__init__(message) | |
class LinkifyIt: | |
"""linkify-it-py dummy class. | |
If ``linkify-it-py`` is not installed, this dummy class will be called. And | |
raise :class:`markdown_it.extra.linkify_it.NotImportedError` when input data | |
is parsed via core.rules (linkify). | |
""" | |
def __init__(self, schemas=None, options=None): | |
pass | |
# def add(self, schema, definition): | |
# raise NotImportedError | |
# def match(self, text): | |
# raise NotImportedError | |
def pretest(self, text): | |
raise NotImportedError | |
# def set(self, options): | |
# raise NotImportedError | |
# def test(self, text): | |
# raise NotImportedError | |
# def test_schema_at(self, text, name, position): | |
# raise NotImportedError | |
# def tlds(self, list_tlds, keep_old=False): | |
# raise NotImportedError |
markdown_it/main.py
Outdated
try: | ||
from linkify_it import LinkifyIt | ||
except (ImportError, ModuleNotFoundError): | ||
from markdown_it.extra import LinkifyIt |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
try: | |
from linkify_it import LinkifyIt | |
except (ImportError, ModuleNotFoundError): | |
from markdown_it.extra import LinkifyIt | |
try: | |
import linkify_it | |
except ModuleNotFoundError: | |
linkify_it = None |
markdown_it/main.py
Outdated
@@ -41,8 +46,7 @@ def __init__( | |||
self.options = {} | |||
self.configure(config) | |||
|
|||
# var LinkifyIt = require('linkify-it') | |||
# self.linkify = LinkifyIt() # TODO maybe see https://github.com/Suor/autolink | |||
self.linkify = LinkifyIt() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.linkify = LinkifyIt() | |
self.linkify = linkify_it.LinkifyIt() if linkify_it else None |
Oh, thanks. 😄 |
markdown_it/rules_core/linkify.py
Outdated
# We scan from the end, to keep position when new tags added. | ||
# Use reversed logic in links start/end match | ||
assert tokens is not None | ||
for i in range(len(tokens))[::-1]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just a preference thing but I personally always found reversed(iterable)
more readable than iterable[::-1]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
markdown_it/rules_core/linkify.py
Outdated
return | ||
|
||
if not state.md.linkify: | ||
raise Exception("Linkify enabed but not installed.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
raise Exception("Linkify enabed but not installed.") | |
raise Exception("Linkify enabled but not installed.") |
There's a typo here which originates from my suggestion. Sorry! Btw, the text was just something I just quickly made up, dont know, maybe we want it to be more elaborate somehow @chrisjsewell ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, maybe it could be justified to make the exception type ModuleNotFoundError
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or ImportError
?
I'm worried about exception type. 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'd vote for ModuleNotFoundError
(a subclass of ImportError
, a bit more precise) as it's equivalent to the root cause of the issue here (the exception we suppress inmarkdown_it/main.py
line 16, and never need to raise unless this line runs).
markdown_it/rules_core/linkify.py
Outdated
while i >= 0: | ||
i -= 1 | ||
if i < 0: | ||
break |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
while i >= 0: | |
i -= 1 | |
if i < 0: | |
break | |
while i >= 1: | |
i -= 1 |
Wouldn't this do the same thing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, same 😲
@hukkinj1 have you got any more notes on this? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks very good to me!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks @tsutsu3 😄
close #5
Implement
linkify
with linkify-it-pyImplementation memo
Iflikify-it-py
is not installed, importdummy LinkifyIt
linkify
rule enabled,linkify
option isTrue
andlikify-it-py
is not installedlikify-it-py
in dependency package