Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 BUG: fixing multiple script installs #11

Merged
merged 3 commits into from
Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/dokieli.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
<link media="all" rel="stylesheet" type="text/css" href="https://dokie.li/media/css/dokieli.css" />
```

```{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.
Expand Down
91 changes: 46 additions & 45 deletions sphinx_comments/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}

Expand All @@ -44,56 +44,57 @@ 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");
}
"""
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");
if (sections !== null) {{
section = sections[sections.length-1];
section.appendChild(script);
}}
commentsRunWhenDOMLoaded(addUtterances);
"""
)
app.add_js_file(None, body=js, kind="utterances")
}}
commentsRunWhenDOMLoaded(addUtterances);
"""
)
app.add_js_file(None, body=js, kind="utterances")


def setup(app):
app.add_config_value("comments_config", {}, "html")

# 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__,
Expand Down
6 changes: 4 additions & 2 deletions tests/test_comments/utterances.html
Original file line number Diff line number Diff line change
Expand Up @@ -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);
</script>