diff options
Diffstat (limited to '_exts/typescriptdomain.py')
-rw-r--r-- | _exts/typescriptdomain.py | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/_exts/typescriptdomain.py b/_exts/typescriptdomain.py index 68777645..83ab26c6 100644 --- a/_exts/typescriptdomain.py +++ b/_exts/typescriptdomain.py @@ -8,6 +8,8 @@ TypeScript domain. import re +from pathlib import Path + from docutils import nodes from typing import List, Optional, Iterable, Dict, Tuple from typing import cast @@ -405,6 +407,8 @@ class LinkingHtmlFormatter(HtmlFormatter): ): return value + if self._bridge.docname is None: + return value if xref is None: return value content = caption if caption is not None else value @@ -509,10 +513,15 @@ class MyPygmentsBridge(PygmentsBridge): def highlight_block( self, source, lang, opts=None, force=False, location=None, **kwargs ): - docname, line = location - self.line = line - self.path = self.builder.env.doc2path(docname) - self.docname = docname + if isinstance(location, tuple): + docname, line = location + self.line = line + self.path = self.builder.env.doc2path(docname) + self.docname = docname + elif isinstance(location, Element): + self.line = location.line + self.path = location.source + self.docname = Path(location.source).stem return super().highlight_block(source, lang, opts, force, location, **kwargs) @@ -567,8 +576,12 @@ literal_reg = re.compile(r"``([^`]+)``") def setup(app): - lexer = BetterTypeScriptLexer() - lexer.add_filter(LinkFilter(app)) - app.add_lexer("tsref", lexer) + + class TsrefLexer(BetterTypeScriptLexer): + def __init__(self, **options): + super().__init__(**options) + self.add_filter(LinkFilter(app)) + + app.add_lexer("tsref", TsrefLexer) app.add_domain(TypeScriptDomain) app.add_builder(MyHtmlBuilder) |