Fixed draft blog posts appearing in tags index

This commit is contained in:
squidfunk 2023-08-10 13:31:17 +02:00
parent 04f1b33571
commit 15c024cbbf
No known key found for this signature in database
GPG Key ID: 5ED40BC4F9C436DF
4 changed files with 20 additions and 4 deletions

View File

@ -34,9 +34,9 @@ from lxml.html import fragment_fromstring, tostring
from mkdocs import utils from mkdocs import utils
from mkdocs.utils.meta import get_data from mkdocs.utils.meta import get_data
from mkdocs.commands.build import _populate_page from mkdocs.commands.build import _populate_page
from mkdocs.contrib.search import SearchIndex from mkdocs.contrib.search.search_index import SearchIndex
from mkdocs.plugins import BasePlugin from mkdocs.plugins import BasePlugin
from mkdocs.structure.files import File, Files from mkdocs.structure.files import File, Files, InclusionLevel
from mkdocs.structure.nav import Link, Section from mkdocs.structure.nav import Link, Section
from mkdocs.structure.pages import Page from mkdocs.structure.pages import Page
from tempfile import gettempdir from tempfile import gettempdir
@ -234,6 +234,10 @@ class BlogPlugin(BasePlugin[BlogConfig]):
# Add post metadata # Add post metadata
self.post_meta_map[file.src_uri] = meta self.post_meta_map[file.src_uri] = meta
# Mark page as excluded, so it's not picked up by other plugins
if not self.config.draft and self._is_draft(file.src_uri):
file.inclusion = InclusionLevel.EXCLUDED
# Sort post metadata by date (descending) # Sort post metadata by date (descending)
self.post_meta_map = dict(sorted( self.post_meta_map = dict(sorted(
self.post_meta_map.items(), self.post_meta_map.items(),

View File

@ -76,6 +76,10 @@ class TagsPlugin(BasePlugin[TagsConfig]):
if not self.config.enabled: if not self.config.enabled:
return return
# Skip, if page is excluded
if page.file.inclusion.is_excluded():
return
# Render tags index page # Render tags index page
if page.file == self.tags_file: if page.file == self.tags_file:
return self._render_tag_index(markdown) return self._render_tag_index(markdown)

View File

@ -34,9 +34,9 @@ from lxml.html import fragment_fromstring, tostring
from mkdocs import utils from mkdocs import utils
from mkdocs.utils.meta import get_data from mkdocs.utils.meta import get_data
from mkdocs.commands.build import _populate_page from mkdocs.commands.build import _populate_page
from mkdocs.contrib.search import SearchIndex from mkdocs.contrib.search.search_index import SearchIndex
from mkdocs.plugins import BasePlugin from mkdocs.plugins import BasePlugin
from mkdocs.structure.files import File, Files from mkdocs.structure.files import File, Files, InclusionLevel
from mkdocs.structure.nav import Link, Section from mkdocs.structure.nav import Link, Section
from mkdocs.structure.pages import Page from mkdocs.structure.pages import Page
from tempfile import gettempdir from tempfile import gettempdir
@ -234,6 +234,10 @@ class BlogPlugin(BasePlugin[BlogConfig]):
# Add post metadata # Add post metadata
self.post_meta_map[file.src_uri] = meta self.post_meta_map[file.src_uri] = meta
# Mark page as excluded, so it's not picked up by other plugins
if not self.config.draft and self._is_draft(file.src_uri):
file.inclusion = InclusionLevel.EXCLUDED
# Sort post metadata by date (descending) # Sort post metadata by date (descending)
self.post_meta_map = dict(sorted( self.post_meta_map = dict(sorted(
self.post_meta_map.items(), self.post_meta_map.items(),

View File

@ -76,6 +76,10 @@ class TagsPlugin(BasePlugin[TagsConfig]):
if not self.config.enabled: if not self.config.enabled:
return return
# Skip, if page is excluded
if page.file.inclusion.is_excluded():
return
# Render tags index page # Render tags index page
if page.file == self.tags_file: if page.file == self.tags_file:
return self._render_tag_index(markdown) return self._render_tag_index(markdown)