Skip to content

Commit

Permalink
Merge pull request #167 from jb-delafosse/fix/url-encode
Browse files Browse the repository at this point in the history
fix(link): some links are urlencoded n times
  • Loading branch information
jb-delafosse authored Apr 23, 2021
2 parents 5c92c1a + 7076fd6 commit 06b123b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ repository.
```yaml
repos:
- repo: https://github.com/jb-delafosse/linky-note
rev: v0.4.3
rev: v0.4.4
hooks:
- id: linky-note
args: ['directory-containing-my-markdown']
Expand Down
7 changes: 6 additions & 1 deletion linky_note/adapters/markdown/marko_modifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,18 @@ def render_link(self, element: Link):
element.children[0].children, element.dest, None
)

@staticmethod
def _encode_once(dest: str) -> str:
decoded = urllib.parse.unquote(dest)
return urllib.parse.quote(decoded)

def build_link_or_wikilink(
self, label: str, dest: str, title: Optional[str] = None
):
if self.config.link_system == LinkSystem.LINK:
if _is_internal_destination(dest):
return MarkoBuilder.build_link(
urllib.parse.quote(dest), label, title
self._encode_once(dest), label, title
)
else:
return MarkoBuilder.build_link(dest, label, title)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build-backend = "poetry.masonry.api"

[tool.poetry]
name = "linky-note"
version = "0.4.3"
version = "0.4.4"
description = "Awesome `linky-note` is a Python cli/package created with https://github.com/TezRomacH/python-package-template"
readme = "README.md"
authors = [
Expand Down
16 changes: 15 additions & 1 deletion tests/unit_tests/adapters/mardown/test_marko_modifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def test_marko_modifier_nominal_link_system(build_ast, mocked_db):

def test_marko_modifier_link_system_url_encode(build_ast, mocked_db):
# Given
# A note referenced by a note whose filename needs to be url encode
# - a reference whose filename needs to be url encode
# - a reference whose filename is already url endoded
source_note = Note(
note_title=NoteTitle("Marketing"), note_path=NotePath("Marketing.md")
)
Expand All @@ -77,6 +78,14 @@ def test_marko_modifier_link_system_url_encode(build_ast, mocked_db):
target_note=source_note,
context=ReferenceContext(f"A reference to {url}"),
),
Reference(
source_note=Note(
note_title=NoteTitle("Content Marketing"),
note_path=NotePath("content%20marketing.md"),
),
target_note=source_note,
context=ReferenceContext(f"A reference to {url}"),
),
)
)
modifier = MarkoModifierImpl(mocked_db(returned_value), ModifyConfig())
Expand All @@ -87,10 +96,15 @@ def test_marko_modifier_link_system_url_encode(build_ast, mocked_db):

# Then
# - The link to the reference is URL encoded
# - an already url encoded link is not re-encoded
assert (
modified_ast.children[10].children[0].children[0].children[0].dest
== "digital%20marketing.md"
)
assert (
modified_ast.children[10].children[1].children[0].children[0].dest
== "content%20marketing.md"
)


def test_marko_modifier_nominal_wikilink_system(build_ast, mocked_db):
Expand Down

0 comments on commit 06b123b

Please sign in to comment.