Skip to content

Commit 7bdd03a

Browse files
committed
Fixed missing titles in views when blog posts have no h1 headings
1 parent 3f87fe0 commit 7bdd03a

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

material/plugins/blog/structure/__init__.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,12 @@ def __init__(self, post: Post, config: MkDocsConfig, files: Files):
143143
self.file = copy(post.file)
144144
self.post = post
145145

146-
# Initialize configuration, contents and metadata
146+
# Set canonical URL, or we can't print excerpts when debugging the
147+
# blog plugin, as the `abs_url` property would be missing
148+
self._set_canonical_url(config.site_url)
149+
150+
# Initialize configuration and metadata
147151
self.config = post.config
148-
self.markdown = post.markdown
149152
self.meta = post.meta
150153

151154
# Initialize authors and categories - note that views usually contain
@@ -189,6 +192,12 @@ def render(self, page: Page, separator: str):
189192
processor: ExcerptTreeprocessor = self.md.treeprocessors[at]
190193
processor.base = page
191194

195+
# Ensure that the excerpt includes a title in its content, since the
196+
# title is linked to the post when rendering - see https://t.ly/5Gg2F
197+
self.markdown = self.post.markdown
198+
if not self.post._title_from_render:
199+
self.markdown = "\n\n".join([f"# {self.post.title}", self.markdown])
200+
192201
# Convert Markdown to HTML and extract excerpt
193202
self.content = self.md.convert(self.markdown)
194203
self.content, *_ = self.content.split(separator, 1)

src/plugins/blog/structure/__init__.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,12 @@ def __init__(self, post: Post, config: MkDocsConfig, files: Files):
143143
self.file = copy(post.file)
144144
self.post = post
145145

146-
# Initialize configuration, contents and metadata
146+
# Set canonical URL, or we can't print excerpts when debugging the
147+
# blog plugin, as the `abs_url` property would be missing
148+
self._set_canonical_url(config.site_url)
149+
150+
# Initialize configuration and metadata
147151
self.config = post.config
148-
self.markdown = post.markdown
149152
self.meta = post.meta
150153

151154
# Initialize authors and categories - note that views usually contain
@@ -189,6 +192,12 @@ def render(self, page: Page, separator: str):
189192
processor: ExcerptTreeprocessor = self.md.treeprocessors[at]
190193
processor.base = page
191194

195+
# Ensure that the excerpt includes a title in its content, since the
196+
# title is linked to the post when rendering - see https://t.ly/5Gg2F
197+
self.markdown = self.post.markdown
198+
if not self.post._title_from_render:
199+
self.markdown = "\n\n".join([f"# {self.post.title}", self.markdown])
200+
192201
# Convert Markdown to HTML and extract excerpt
193202
self.content = self.md.convert(self.markdown)
194203
self.content, *_ = self.content.split(separator, 1)

0 commit comments

Comments
 (0)