Skip to content

Commit 2a74644

Browse files
committed
Use document title as isso thread title
1 parent 1abf713 commit 2a74644

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

sphinxnotes/isso/__init__.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
if TYPE_CHECKING:
2020
from sphinx.application import Sphinx
21+
from sphinx.util import logging
2122

2223
__title__= 'sphinxnotes-isso'
2324
__license__ = 'BSD',
@@ -36,6 +37,8 @@
3637
'isso_avatar_fg', 'isso_vote', 'isso_vote_levels',
3738
'isso_feed']
3839

40+
logger = logging.getLogger(__name__)
41+
3942
def ext_config_to_isso_config(key:str, value:Any) -> Tuple[str,str]:
4043
assert key in CONFIG_ITEMS
4144
key = 'data-' + key.replace('_', '-')
@@ -55,8 +58,11 @@ def visit(self, node):
5558
kwargs = {
5659
'data-isso-id': node['thread-id'],
5760
}
61+
if node.get('thread-title'):
62+
kwargs['data-title'] = node['thread-title']
5863
self.body.append(self.starttag(node, 'section', '', **kwargs))
5964

65+
6066
@staticmethod
6167
def depart(self, _):
6268
self.body.append('</section>')
@@ -66,7 +72,8 @@ class IssoDirective(Directive):
6672
"""Isso ".. isso::" rst directive."""
6773

6874
option_spec = {
69-
'id': directives.unchanged
75+
'id': directives.unchanged,
76+
'title': directives.unchanged,
7077
}
7178

7279
def run(self):
@@ -77,11 +84,23 @@ def run(self):
7784

7885
node = IssoNode()
7986
node['ids'] = ['isso-thread']
80-
node['thread-id'] = self.options.get('id') or \
81-
'/' + self.state.document.settings.env.docname
87+
if self.options.get('id'):
88+
thread_id = self.options.get('id')
89+
if not thread_id.startswith('/'):
90+
logger.warning('isso thread-id should starts with slash', location=node)
91+
node['thread-id'] = thread_id
92+
else:
93+
node['thread-id'] = '/' + self.state.document.settings.env.docname
94+
if self.options.get('title'):
95+
node['thread-title'] = self.options.get('title')
96+
else:
97+
title = self.state.document.next_node(nodes.title)
98+
if title:
99+
node['thread-title'] = title.astext()
82100

83101
return [node]
84102

103+
85104
def on_html_page_context(app:Sphinx, pagename:str, templatename:str, context,
86105
doctree:nodes.document) -> None:
87106
"""Called when the HTML builder has created a context dictionary to render a template with.

0 commit comments

Comments
 (0)