-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add options for checking the length of meta descriptions (#182)
* doc: Add options for checking the length of meta descriptions * feat: Update config_scheme * clean: Avoid defining default config values more than once * feat: Implement core functionality * clean: Rename option check_length to enable_checks This makes the option name more future proof in case the plugin will perform more validations besides checking the length at some point. * test: Add minimal safeguard mkdocs configuration file * clean: Review warning messages copy * clean: Add warning for missing meta descriptions * test: Add tests for enable_checks * doc: Update CHANGELOG.md for v2.1.0
- Loading branch information
Paulo Ribeiro
authored
Oct 1, 2022
1 parent
bcc5ff5
commit 9ed7df1
Showing
17 changed files
with
178 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from string import Template | ||
|
||
from .common import logger | ||
|
||
|
||
class Checker: | ||
"""Checks meta descriptions against general SEO recommendations.""" | ||
_initialized = False | ||
_check = False | ||
_min_length = 50 | ||
_max_length = 160 | ||
_template_warning_missing = Template( | ||
"Meta description not found: $page") | ||
_template_warning_length = Template( | ||
"Meta description $character_count character$plural $comparative than $limit: $page") | ||
|
||
def _check_length(self, page): | ||
length = len(page.meta.get("description", "")) | ||
if length == 0: | ||
logger.write(logger.Warning, self._template_warning_missing.substitute(page=page.file.src_path)) | ||
return # Skip length check | ||
elif length < self._min_length: | ||
diff = self._min_length - length | ||
logger.write(logger.Warning, self._template_warning_length.substitute(character_count=diff, | ||
plural="s"[:diff != 1], | ||
comparative="shorter", | ||
limit=self._min_length, | ||
page=page.file.src_path)) | ||
elif length > self._max_length: | ||
diff = length - self._max_length | ||
logger.write(logger.Warning, self._template_warning_length.substitute(character_count=diff, | ||
plural="s"[:diff != 1], | ||
comparative="longer", | ||
limit=self._max_length, | ||
page=page.file.src_path)) | ||
|
||
def initialize(self, config): | ||
self._check = config.get("enable_checks") | ||
self._min_length = config.get("min_length") | ||
self._max_length = config.get("max_length") | ||
self._initialized = True | ||
|
||
def check(self, page): | ||
if not self._initialized: | ||
logger.write(logger.Warning, "'LengthChecker' object not initialized yet, using default configurations") | ||
if self._check: | ||
self._check_length(page) | ||
|
||
|
||
checker = Checker() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
description: Long meta description, long meta description. | ||
--- | ||
|
||
# Heading 1 | ||
|
||
First paragraph. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Heading 1 | ||
|
||
## Heading 2 | ||
|
||
Page without meta description. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
description: Short meta description. | ||
--- | ||
|
||
# Heading 1 | ||
|
||
First paragraph. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
site_name: Test mkdocs-meta-descriptions-plugin | ||
|
||
theme: | ||
name: material | ||
|
||
plugins: | ||
- search | ||
- meta-descriptions: | ||
enable_checks: true | ||
min_length: 25 | ||
max_length: 35 | ||
|
||
markdown_extensions: | ||
- admonition |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
site_name: Test mkdocs-meta-descriptions-plugin | ||
site_description: "Value of site_description on mkdocs.yml" | ||
|
||
theme: | ||
name: material | ||
|
||
plugins: | ||
- search | ||
- meta-descriptions: | ||
enable_checks: true | ||
min_length: 25 | ||
max_length: 35 | ||
|
||
markdown_extensions: | ||
- admonition |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters