-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathnotion.py
More file actions
52 lines (41 loc) · 1.34 KB
/
notion.py
File metadata and controls
52 lines (41 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""A directive to generate an iframe with a Notion page."""
import urllib.parse
from docutils import nodes
from docutils.parsers.rst import Directive
from sphinx.application import Sphinx
from sphinx.util import logging
logger = logging.getLogger(__name__)
TEMPLATE: str = """
<iframe src="{url}" width="100%" height="1200" frameborder="0" allowfullscreen>
</iframe>
"""
class Notion(Directive):
has_content = True
final_argument_whitespace = False
def run(self):
if not self.content:
raise self.error("Notion directive requires a URL")
if len(self.content) > 1:
raise self.error("Notion directive only accepts a single URL")
url = self.content[0]
# Basic validation that it's a Notion URL
if (
not url.startswith(
("https://notion.site", "https://www.notion.so")
)
and "notion" not in url
):
raise self.error("URL does not appear to be a valid Notion URL")
para = nodes.raw(
"",
TEMPLATE.format(url=urllib.parse.quote(url, safe=":/?&=+")),
format="html",
)
return [para]
def setup(app: Sphinx):
app.add_directive("notion", Notion)
return {
"version": "0.1",
"parallel_read_safe": True,
"parallel_write_safe": True,
}