Skip to content

Commit

Permalink
Axe code that removes lazy loading from 1st image (#79)
Browse files Browse the repository at this point in the history
This code caused an issue with the HTML output and is better suited to a
plugin
  • Loading branch information
johnfraney committed May 2, 2024
1 parent 3a08b2a commit 6cd0541
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 45 deletions.
4 changes: 0 additions & 4 deletions blurry/markdown/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from blurry.utils import content_path_to_url
from blurry.utils import convert_relative_path_in_markdown_file_to_pathname
from blurry.utils import path_to_url_pathname
from blurry.utils import remove_lazy_loading_from_first_image
from blurry.utils import resolve_relative_path_in_markdown


Expand Down Expand Up @@ -178,9 +177,6 @@ def convert_markdown_file_to_html(filepath: Path) -> tuple[str, dict[str, Any]]:
if not is_str(html):
raise Exception(f"Expected html to be a string but got: {type(html)}")

# Post-process HTML
html = remove_lazy_loading_from_first_image(html)

# Seed front_matter with schema_data from config file
front_matter: dict[str, Any] = dict(SETTINGS.get("SCHEMA_DATA", {}))
front_matter.update(state.env.get("front_matter", {}))
Expand Down
18 changes: 0 additions & 18 deletions blurry/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from pathlib import Path

from selectolax.parser import HTMLParser

from blurry.settings import get_build_directory
from blurry.settings import get_content_directory
from blurry.settings import SETTINGS
Expand Down Expand Up @@ -116,19 +114,3 @@ def format_schema_data(schema_data: dict) -> dict:
formatted_schema_data = {"@context": "https://schema.org"}
formatted_schema_data.update(schema_data)
return formatted_schema_data


def remove_lazy_loading_from_first_image(html: str) -> str:
parser = HTMLParser(html, use_meta_tags=False)
if not parser.body or not parser.body.html:
raise Exception("Could not parse HTML")
first_img_tag = parser.css_first("img")
if not first_img_tag:
return html
updated_tag = first_img_tag
try:
del updated_tag.attrs["loading"] # type: ignore
first_img_tag.replace_with(HTMLParser(updated_tag.html).body.child) # type: ignore
except KeyError:
pass
return parser.body.html
4 changes: 2 additions & 2 deletions docs/content/content/images.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ For more information, see [the Lighthouse documentation on image aspect ratios](

### Lazy loading

Blurry adds lazy loading (`loading="lazy"`) to all images in body content *except the first image*.
Blurry adds lazy loading (`loading="lazy"`) to all images in body content.

This provides a page speed boost by waiting to load most images on a page until they're needed while preserving a main image that may appear above the fold.
This provides a page speed boost by waiting to load most images on a page until they're needed.

### Figure caption

Expand Down
21 changes: 0 additions & 21 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from unittest import mock

import pytest
from selectolax.parser import HTMLParser

from blurry.settings import get_build_directory
from blurry.settings import get_content_directory
Expand All @@ -15,7 +14,6 @@
from blurry.utils import format_schema_data
from blurry.utils import get_domain_with_scheme
from blurry.utils import path_to_url_pathname
from blurry.utils import remove_lazy_loading_from_first_image
from blurry.utils import sort_directory_file_data_by_date


Expand Down Expand Up @@ -152,22 +150,3 @@ def test_format_schema_data():
"@context": "https://schema.org",
"@type": "BlogPosting",
}


def test_remove_lazy_loading_from_first_image():
html = """
<body>
<picture class="one">
<source src="one.avif">
<img src="one.png" loading="lazy">
</picture>
<picture class="two">
<source src="two.avif">
<img src="two.png" loading="lazy">
</picture>
</body>
"""
updated_html = remove_lazy_loading_from_first_image(html)
parser = HTMLParser(updated_html)
assert parser.css_first("picture.one img").attributes.get("loading") is None
assert parser.css_first("picture.two img").attributes.get("loading") == "lazy"

0 comments on commit 6cd0541

Please sign in to comment.