Skip to content

Respect an explicit excerpt of 0 in page front-matter - #338

Open
Muhtasim-Munif-Fahim wants to merge 1 commit into
sunpy:mainfrom
Muhtasim-Munif-Fahim:fix/front-matter-excerpt-zero
Open

Respect an explicit excerpt of 0 in page front-matter#338
Muhtasim-Munif-Fahim wants to merge 1 commit into
sunpy:mainfrom
Muhtasim-Munif-Fahim:fix/front-matter-excerpt-zero

Conversation

@Muhtasim-Munif-Fahim

Copy link
Copy Markdown

Fixes #317.

The bug

CheckFrontMatter guards the excerpt fallback with a truthiness check:

if not metadata.get("excerpt"):
    blog = Blog(self.app)
    node["excerpt"] = blog.post_auto_excerpt

0 is falsy, so an explicit :excerpt: 0 is indistinguishable from no excerpt at all and gets overwritten with post_auto_excerpt. The first paragraph of the post then leaks into every postlist rendered with :excerpts:, which is what #317 describes.

The same option given to the post directive already behaves correctly, because process_posts guards the identical fallback with is None:

if node["excerpt"] is None:
    node["excerpt"] = auto_excerpt

So the two ways of declaring a post disagree. This aligns the front-matter path with the directive path.

Reproduction

Two posts, identical except for how the excerpt is declared:

.. post:: 2020-12-01
   :excerpt: 0
:blogpost: true
:date: December 01, 2020
:excerpt: 0

Before this change the directive post correctly renders no excerpt, while the front-matter post renders its first paragraph. After, both render nothing.

Tests

Adds roots/test-excerpt and src/ablog/tests/test_excerpt.py covering four cases:

  • an explicit 0 suppresses the excerpt,
  • a non-zero count still yields that many paragraphs,
  • an absent value still falls back to post_auto_excerpt,
  • an explicit 0 wins over a non-default post_auto_excerpt.

The first and last fail without the one-line change; the middle two are controls that pass either way. Full suite is 13 passed on Sphinx 9.1.0.

One note

The new tests carry a filterwarnings mark:

pytestmark = pytest.mark.filterwarnings(
    "ignore:'ablog.post.CheckFrontMatter.app' is deprecated"
)

CheckFrontMatter reaches the app through SphinxTransform.app, which Sphinx 9 deprecates (the replacement is the private env._app). Since the suite turns warnings into errors, any test exercising the front-matter path trips it. That is a pre-existing compatibility issue orthogonal to this bug, and migrating to a private attribute felt like your call rather than something to smuggle into a bugfix — happy to fold it in here or open a separate issue, whichever you prefer.

🤖 Generated with Claude Code

`CheckFrontMatter` guarded the excerpt fallback with a truthiness check, so
an explicit `:excerpt: 0` was indistinguishable from no excerpt at all and
was overwritten with `post_auto_excerpt`. The first paragraph of the post
then leaked into every `postlist` rendered with `:excerpts:`.

The same option given to the `post` directive already behaved correctly,
because `process_posts` guards the identical fallback with `is None`. This
aligns the front-matter path with that behaviour.

Adds `roots/test-excerpt` and `test_excerpt.py` covering the explicit 0, a
non-zero count, the absent-value fallback, and an explicit 0 against a
non-default `post_auto_excerpt`. The first and last fail without this change.

Fixes sunpy#317

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

postlist excerpts ignores post excerpt length of 0

1 participant