From 64ae2e890e2f35000f4819d59f865bb56e47f2ba Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Tue, 11 Aug 2020 16:52:41 -0700 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=90=9B=20BUG:=20fixing=20multiple=20s?= =?UTF-8?q?cript=20installs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/conf.py | 2 +- sphinx_comments/__init__.py | 95 ++++++++++++++++++------------------- 2 files changed, 48 insertions(+), 49 deletions(-) diff --git a/doc/conf.py b/doc/conf.py index 74397c9..d7051c3 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -45,7 +45,7 @@ # "utterances": { # "repo": "executablebooks/sphinx-comments", # }, - # "dokieli": True + "dokieli": True } # Add any paths that contain templates here, relative to this directory. templates_path = ["_templates"] diff --git a/sphinx_comments/__init__.py b/sphinx_comments/__init__.py index ff39af6..cdd4a27 100644 --- a/sphinx_comments/__init__.py +++ b/sphinx_comments/__init__.py @@ -11,16 +11,16 @@ def shp_static_path(app): app.config.html_static_path.append(static_path) -def activate_comments(app, pagename, templatename, context, doctree): +def activate_comments(app, config): """Activate commenting on each page.""" # Grab config instances - config = app.config.comments_config.copy() - if not isinstance(config, (dict, type(None))): + com_config = app.config.comments_config.copy() + if not isinstance(com_config, (dict, type(None))): raise ValueError("Comments configuration must be a dictionary.") - ut_config = config.get("utterances") - dk_config = config.get("dokieli") - ht_config = config.get("hypothesis") + ut_config = com_config.get("utterances") + dk_config = com_config.get("dokieli") + ht_config = com_config.get("hypothesis") extra_config = {"async": "async"} @@ -44,48 +44,47 @@ def activate_comments(app, pagename, templatename, context, doctree): raise ValueError("To use utterances, you must provide a repository.") repo = ut_config["repo"] - # Utterances requires a script + config in a specific place, so add to doctree - if doctree: - dom = """ - var commentsRunWhenDOMLoaded = cb => { - if (document.readyState != 'loading') { - cb() - } else if (document.addEventListener) { - document.addEventListener('DOMContentLoaded', cb) - } else { - document.attachEvent('onreadystatechange', function() { - if (document.readyState == 'complete') cb() - }) - } + # Utterances requires a script + config in a specific place, so do this w/ JS + dom = """ + var commentsRunWhenDOMLoaded = cb => { + if (document.readyState != 'loading') { + cb() + } else if (document.addEventListener) { + document.addEventListener('DOMContentLoaded', cb) + } else { + document.attachEvent('onreadystatechange', function() { + if (document.readyState == 'complete') cb() + }) } - """ - issue_term = ut_config.get("issue-term", "pathname") - theme = ut_config.get("theme", "github-light") - label = ut_config.get("label", "💬 comment") - crossorigin = ut_config.get("crossorigin", "anonymous") - js = dedent( - f""" - {dom} - var addUtterances = () => {{ - var script = document.createElement("script"); - script.type = "text/javascript"; - script.src = "https://utteranc.es/client.js"; - script.async = "async"; - - script.setAttribute("repo", "{repo}"); - script.setAttribute("issue-term", "{issue_term}"); - script.setAttribute("theme", "{theme}"); - script.setAttribute("label", "{label}"); - script.setAttribute("crossorigin", "{crossorigin}"); - - sections = document.querySelectorAll("div.section"); - section = sections[sections.length-1]; - section.appendChild(script); - }} - commentsRunWhenDOMLoaded(addUtterances); - """ - ) - app.add_js_file(None, body=js, kind="utterances") + } + """ + issue_term = ut_config.get("issue-term", "pathname") + theme = ut_config.get("theme", "github-light") + label = ut_config.get("label", "💬 comment") + crossorigin = ut_config.get("crossorigin", "anonymous") + js = dedent( + f""" + {dom} + var addUtterances = () => {{ + var script = document.createElement("script"); + script.type = "text/javascript"; + script.src = "https://utteranc.es/client.js"; + script.async = "async"; + + script.setAttribute("repo", "{repo}"); + script.setAttribute("issue-term", "{issue_term}"); + script.setAttribute("theme", "{theme}"); + script.setAttribute("label", "{label}"); + script.setAttribute("crossorigin", "{crossorigin}"); + + sections = document.querySelectorAll("div.section"); + section = sections[sections.length-1]; + section.appendChild(script); + }} + commentsRunWhenDOMLoaded(addUtterances); + """ + ) + app.add_js_file(None, body=js, kind="utterances") def setup(app): @@ -93,7 +92,7 @@ def setup(app): # Add our static path app.connect("builder-inited", shp_static_path) - app.connect("html-page-context", activate_comments) + app.connect("config-inited", activate_comments) return { "version": __version__, From b94e7e53a68d91d557252ffff13444650c3b7c69 Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Tue, 11 Aug 2020 16:59:05 -0700 Subject: [PATCH 2/3] dokieli warning --- doc/conf.py | 2 +- doc/dokieli.md | 4 ++++ sphinx_comments/__init__.py | 6 ++++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/doc/conf.py b/doc/conf.py index d7051c3..74397c9 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -45,7 +45,7 @@ # "utterances": { # "repo": "executablebooks/sphinx-comments", # }, - "dokieli": True + # "dokieli": True } # Add any paths that contain templates here, relative to this directory. templates_path = ["_templates"] diff --git a/doc/dokieli.md b/doc/dokieli.md index f82f347..a583f28 100644 --- a/doc/dokieli.md +++ b/doc/dokieli.md @@ -5,6 +5,10 @@ ``` +```{warning} +Dokieli is experimental and may not behave as expected right now! +``` + Dokieli is an open source comments and annotation engine that follows web standards and allows you to have full control over where your comments are aggregated and who has access. Dokieli is activated on this page. You can see the web overlay by clicking on the hamburger menu in the upper-right corner of this page. diff --git a/sphinx_comments/__init__.py b/sphinx_comments/__init__.py index cdd4a27..d465bd6 100644 --- a/sphinx_comments/__init__.py +++ b/sphinx_comments/__init__.py @@ -78,8 +78,10 @@ def activate_comments(app, config): script.setAttribute("crossorigin", "{crossorigin}"); sections = document.querySelectorAll("div.section"); - section = sections[sections.length-1]; - section.appendChild(script); + if (sections !== null) {{ + section = sections[sections.length-1]; + section.appendChild(script); + }} }} commentsRunWhenDOMLoaded(addUtterances); """ From c4bb3e59fe37342d5c70b7ebc3d1ab5865793ae7 Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Tue, 11 Aug 2020 17:00:37 -0700 Subject: [PATCH 3/3] utterances test --- tests/test_comments/utterances.html | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_comments/utterances.html b/tests/test_comments/utterances.html index 7468ab1..af5ae70 100644 --- a/tests/test_comments/utterances.html +++ b/tests/test_comments/utterances.html @@ -24,8 +24,10 @@ script.setAttribute("crossorigin", "anonymous"); sections = document.querySelectorAll("div.section"); - section = sections[sections.length-1]; - section.appendChild(script); + if (sections !== null) { + section = sections[sections.length-1]; + section.appendChild(script); + } } commentsRunWhenDOMLoaded(addUtterances);