Skip to content

Commit

Permalink
use toctree for og:description if empty
Browse files Browse the repository at this point in the history
  • Loading branch information
shimizukawa authored Feb 24, 2022
1 parent b0e33de commit d00ef23
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions source/ext/ogtag.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@

class Visitor:

def __init__(self, document):
def __init__(self, document, skip_toctree=True):
self.document = document
self.skip_toctree = skip_toctree
self.text_list = []
self.images = []
self.n_sections = 0

def dispatch_visit(self, node):
# toctreeは飛ばす
if isinstance(node, addnodes.compact_paragraph) and node.get('toctree'):
if (self.skip_toctree and
isinstance(node, addnodes.compact_paragraph) and
node.get('toctree')):
raise nodes.SkipChildren

# 画像を収集
Expand All @@ -38,6 +41,7 @@ def dispatch_departure(self, node):
def get_og_description(self):
# TODO: 何文字までが良いのか?
text = ' '.join(self.text_list)
text = text.replace('\n', ' ')
if len(text) > 200:
text = text[:197] + '...'
return text
Expand All @@ -58,6 +62,9 @@ def get_og_tags(context, doctree, config):
# collection
visitor = Visitor(doctree)
doctree.walkabout(visitor)
if not visitor.get_og_description():
visitor = Visitor(doctree, skip_toctree=False)
doctree.walkabout(visitor)

# og:title
try:
Expand Down

0 comments on commit d00ef23

Please sign in to comment.