Commit 96ed6a0d authored by Patrik Dufresne's avatar Patrik Dufresne

Fix link resolution for image to use PRIMARY_ALT

Images/Attachments are only generated into the primary alt folder and
not into the secondary alt. So image url need to get resolved using the
primary alt.
parent 0593bc1b
Pipeline #327 passed with stages
in 1 minute and 14 seconds
......@@ -7,17 +7,16 @@ Created on Jun 8, 2018
import markdown
from markdown.extensions import Extension
from markdown.inlinepatterns import LinkPattern
from markupsafe import Markup
import types
from weakref import ref as weakref
from werkzeug.urls import url_parse
from lektor.context import get_ctx
from lektor.environment import PRIMARY_ALT
from lektor.pluginsystem import Plugin
from lektor.types import Type
from lektor.utils import bool_from_string
from markupsafe import Markup
from werkzeug.urls import url_parse
SECTION_EXTENSIONS = "extensions"
SECTION_MARKDOWN = "markdown"
......@@ -26,15 +25,25 @@ DEFAULT_EXTENTIONS = {
}
def sanitize_url(self, link):
def sanitize_link(self, link):
"""
Patched function to resolve the url using Lektor.
"""
if get_ctx() and get_ctx().record is not None:
url = url_parse(link)
if not url.scheme:
link = get_ctx().record.url_to(link, base_url=get_ctx().base_url)
return LinkPattern.sanitize_url(self, link)
def sanitize_image(self, link):
"""
Patched function to resolve the url using Lektor.
"""
if get_ctx() and get_ctx().record is not None:
url = url_parse(link)
if not url.scheme:
link = get_ctx().record.url_to("!" + link,
base_url=get_ctx().base_url)
link = get_ctx().record.url_to(link, alt=PRIMARY_ALT, base_url=get_ctx().base_url)
return LinkPattern.sanitize_url(self, link)
......@@ -45,17 +54,17 @@ class LektorMarkdownExtension(Extension):
markdown rendered (mistune).
"""
def _patch(self, p):
def _patch(self, p, func):
"""
Monkey patch the sanitize_url method.
"""
p.sanitize_url = types.MethodType(sanitize_url, p)
p.sanitize_url = types.MethodType(func, p)
def extendMarkdown(self, md, md_globals):
self._patch(md.inlinePatterns['link'])
self._patch(md.inlinePatterns['image_link'])
self._patch(md.inlinePatterns['image_reference'])
self._patch(md.inlinePatterns['reference'])
self._patch(md.inlinePatterns['link'], sanitize_link)
self._patch(md.inlinePatterns['reference'], sanitize_link)
self._patch(md.inlinePatterns['image_link'], sanitize_image)
self._patch(md.inlinePatterns['image_reference'], sanitize_image)
class PythonMarkdownConfig(object):
......
......@@ -42,14 +42,21 @@ class TestLektorPythonMarkdown(unittest.TestCase):
# The output changes depending on the version of python-markdown uses.
# assert '<pre class="codehilite"><code class="linenums">code here</code></pre>' in html
# Check url & image substitution
assert '<a href="/fr/sub-page">Link to Sub Page</a>' in html
assert '<a href="/fr/sub-page-slug">Link to Slug</a>' in html
assert '<a href="/fr/invalid-page">Link to Invalid Page</a>' in html
assert '<a href="/fr/sub-page/">Link to Sub Page</a>' in html
assert '<a href="/fr/slug-url/">Link to Slug</a>' in html
assert '<a href="/invalid-page">Link to Invalid Page</a>' in html
assert '<img alt="alttxt" src="/fr/logo.png" />' in html
# Check references
assert '<a href="/fr/sub-page/" title="Sub Page">Sub Page</a>' in html
assert '<a href="http://search.yahoo.com/" title="Yahoo Search">Yahoo</a>' in html
def test_basic_alt_en(self):
failures = self.builder.build_all()
assert not failures
page_path = os.path.join(self.builder.destination_path, 'en/index.html')
html = open(page_path).read()
print(html)
assert '<img alt="alttxt" src="/fr/logo.png" />' in html
if __name__ == "__main__":
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment