Skip to content
This repository was archived by the owner on Nov 23, 2021. It is now read-only.

Commit 59c5059

Browse files
committed
Add ability to embed youtube videos easily
1 parent a512541 commit 59c5059

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed

css/ditto.css

+3
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,9 @@ input[type=search] {
318318
margin-bottom: 0;
319319
}
320320

321+
#content .youtube_video {
322+
margin-top: 20px;
323+
}
321324

322325
#hide {
323326
display: none;

docs/features.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Features
2+
3+
- Embed Youtube videos
4+
5+
6+
## Embed Youtube videos
7+
8+
Use `[ditto:youtube:<youtube url>]` to embed youtube videos:
9+
10+
[ditto:youtube:https://www.youtube.com/watch?v=9CS7j5I6aOc]
11+
12+
[ditto:youtube:https://www.youtube.com/watch?v=9CS7j5I6aOc]

index.html

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<title>ditto</title> <!-- EDIT ME!! -->
4+
<title>ditto</title>
55

66
<!-- JQUERY -->
77
<script src="https://code.jquery.com/jquery-1.11.0.min.js"></script>
@@ -37,7 +37,6 @@
3737
<!-- DITTO -->
3838
<link rel="stylesheet" href="css/ditto.css">
3939
<script src="js/ditto.js"></script>
40-
4140
</head>
4241
<body>
4342
<!-- ESSENTIAL -->

js/ditto.js

+38
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,43 @@ $(function($) {
267267
}
268268
}
269269

270+
function youtube_url_extract(data) {
271+
var yt_regex = /(https?\:\/\/)?(www\.youtube\.com|youtu\.?be)\/.+/g;
272+
var yt_url = String(data.match(yt_regex));
273+
yt_url = yt_url.replace(/]/g, "");
274+
return yt_url;
275+
}
276+
277+
function youtube_url_to_embed(data) {
278+
return data = data.replace(/watch\?v\=/g, "embed/");
279+
}
280+
281+
function create_youtube_embeds(data) {
282+
// replaces [ditto:youtube:<some youtube link>]
283+
// with a proper youtube embed iframe
284+
var token_regex = /\[ditto\:youtube:(https?\:\/\/)?(www\.youtube\.com|youtu\.?be)\/.+\]/g;
285+
var yt_url = youtube_url_extract(data);
286+
var yt_embed_url = youtube_url_to_embed(yt_url);
287+
288+
// youtube embed html
289+
var embed_html = `<div class="youtube_video">
290+
<div style="position:relative;height:0;padding-bottom:56.25%">
291+
<iframe src="<link>?ecver=2"
292+
width="640"
293+
height="360"
294+
frameborder="0"
295+
style="position:absolute;width:100%;height:100%;left:0"
296+
allowfullscreen>
297+
</iframe>
298+
</div>
299+
</div>`;
300+
embed_html = embed_html.replace("<link>", yt_embed_url);
301+
302+
// replace match code with youtube video
303+
data = data.replace(token_regex, embed_html);
304+
return data;
305+
}
306+
270307
function normalize_paths() {
271308
// images
272309
ditto.content_id.find("img").map(function() {
@@ -387,6 +424,7 @@ $(function($) {
387424
function compile_into_dom(path, data, cb) {
388425
hide_errors();
389426

427+
data = create_youtube_embeds(data);
390428
data = marked(escape_html(data));
391429
data = unescape_html(data);
392430
ditto.content_id.html(data);

sidebar.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ version 0.13
1111
- [Why use ditto?](#docs/why_use_ditto)
1212
- [How do I run ditto locally?](#docs/how_do_i_run_ditto_locally)
1313
- [How I write equations in ditto?](#docs/maths_demo)
14+
- [Features](#docs/features)
1415

1516
## Projects that use ditto
1617
- [playground](http://chutsu.github.io/playground)

0 commit comments

Comments
 (0)