Skip to content
This repository was archived by the owner on Mar 5, 2024. It is now read-only.

Commit 627a593

Browse files
nlarewi80and
authored andcommitted
Added codepen directive with correct script handling
Signed-off-by: Andrew Aldridge <[email protected]>
1 parent 7650912 commit 627a593

File tree

5 files changed

+69
-2
lines changed

5 files changed

+69
-2
lines changed

sphinxext/codepen.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from docutils import nodes
2+
from docutils.parsers.rst import Directive, directives
3+
4+
5+
class codepen(nodes.Element):
6+
pass
7+
8+
9+
def visit_codepen(self, node):
10+
slug = self.attval(node['slug'][0])
11+
attributes = {
12+
'data-pen-title': slug,
13+
'data-slug-hash': slug,
14+
'data-height': '600',
15+
'data-theme-id': '32535',
16+
'data-default-tab': 'js,result',
17+
'data-user': 'mongodb-docs',
18+
'data-embed-version': '2',
19+
'data-editable': 'true'
20+
}
21+
start_tag = self.starttag(node, 'p', CLASS='codepen', **attributes)
22+
self.body.append(start_tag)
23+
24+
25+
def depart_codepen(self, node):
26+
slug = self.attval(node['slug'][0])
27+
link = 'https://codepen.io/mongodb-docs/pen/{slug}'.format(slug=slug)
28+
redirect = '''<a href="{link}">
29+
See this example on codepen: {slug}
30+
</a>'''.format(link=link, slug=slug)
31+
self.body.append('{redirect}</p>\n'.format(redirect=redirect))
32+
33+
34+
class Codepen(Directive):
35+
"""
36+
Embed a mongodb-docs codepen with the provided slug
37+
"""
38+
has_content = False
39+
required_arguments = 1
40+
optional_arguments = 0
41+
42+
def run(self):
43+
pen = codepen('')
44+
pen['slug'] = [self.arguments[0]]
45+
return [pen]
46+
47+
48+
def setup(app):
49+
app.add_node(codepen, html=(
50+
visit_codepen, depart_codepen
51+
))
52+
directives.register_directive('codepen', Codepen)
53+
54+
return {
55+
'parallel_read_safe': False,
56+
'parallel_write_safe': True,
57+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function setup() {
2+
const codepenBlocks = document.getElementsByClassName('codepen');
3+
if (codepenBlocks.length) {
4+
const codepenScript = document.createElement('script');
5+
codepenScript.src = 'https://production-assets.codepen.io/assets/embed/ei.js';
6+
document.body.appendChild(codepenScript);
7+
}
8+
}

themes/mongodb/src/js/controller.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as componentCodepen from './componentCodepen';
12
import * as componentCopyButtons from './componentCopyButtons';
23
import * as componentFastLoad from './componentFastLoad';
34
import * as componentFeedback from './componentFeedback';
@@ -26,6 +27,7 @@ const fastNav = new FastNav();
2627

2728
$(() => {
2829
fastNav.register(componentCopyButtons);
30+
fastNav.register(componentCodepen);
2931
fastNav.register(componentFastLoad);
3032
fastNav.register(componentFeedback);
3133
fastNav.register(componentLightbox);

0 commit comments

Comments
 (0)