Commit f43fa51e authored by Robert Knight's avatar Robert Knight Committed by GitHub

Merge pull request #508 from SteelWagstaff/master

HTML5 Audio elements
parents c8e523f1 ce7e2d6a
'use strict'; 'use strict';
/**
* Return an HTML5 audio player with the given src URL.
*/
function audioElement(src) {
var html5audio = document.createElement('audio');
html5audio.controls = true;
html5audio.src = src;
return html5audio;
}
/** /**
* Return an iframe DOM element with the given src URL. * Return an iframe DOM element with the given src URL.
*/ */
...@@ -24,8 +35,8 @@ function vimeoEmbed(id) { ...@@ -24,8 +35,8 @@ function vimeoEmbed(id) {
} }
/** /**
* A list of functions that return an "embed" DOM element (e.g. an <iframe>) * A list of functions that return an "embed" DOM element (e.g. an <iframe> or
* for a given link. * an html5 <audio> element) for a given link.
* *
* Each function either returns `undefined` if it can't generate an embed for * Each function either returns `undefined` if it can't generate an embed for
* the link, or a DOM element if it can. * the link, or a DOM element if it can.
...@@ -88,6 +99,15 @@ var embedGenerators = [ ...@@ -88,6 +99,15 @@ var embedGenerators = [
} }
return null; return null;
}, },
// Matches URLs that end with .mp3, .ogg, or .wav (assumed to be audio files)
function html5audioFromMp3Link(link) {
if (link.pathname.endsWith('.mp3') || link.pathname.endsWith('.ogg') || link.pathname.endsWith('.wav')) {
return audioElement(link.href);
}
return null;
},
]; ];
/** /**
...@@ -115,7 +135,7 @@ function embedForLink(link) { ...@@ -115,7 +135,7 @@ function embedForLink(link) {
* *
* If the given link element is a link to an embeddable media and if its link * If the given link element is a link to an embeddable media and if its link
* text is the same as its href then it will be replaced in the DOM with an * text is the same as its href then it will be replaced in the DOM with an
* embed (e.g. an <iframe>) of the same media. * embed (e.g. an <iframe> or html5 <audio> element) of the same media.
* *
* If the link text is different from the href, then the link will be left * If the link text is different from the href, then the link will be left
* untouched. We want to convert links like these from the Markdown source into * untouched. We want to convert links like these from the Markdown source into
...@@ -138,14 +158,12 @@ function replaceLinkWithEmbed(link) { ...@@ -138,14 +158,12 @@ function replaceLinkWithEmbed(link) {
if (link.href !== link.textContent) { if (link.href !== link.textContent) {
return; return;
} }
var embed = embedForLink(link); var embed = embedForLink(link);
if (embed) { if (embed){
link.parentElement.replaceChild(embed, link); link.parentElement.replaceChild(embed, link);
} }
} }
/** /**
* Replace all embeddable link elements beneath the given element with embeds. * Replace all embeddable link elements beneath the given element with embeds.
* *
......
...@@ -90,6 +90,29 @@ describe('media-embedder', function () { ...@@ -90,6 +90,29 @@ describe('media-embedder', function () {
}); });
}); });
it('replaces audio links with html5 audio elements', function() {
var urls = [
'https://archive.org/download/testmp3testfile/mpthreetest.mp3',
'https://archive.org/download/testmp3testfile/mpthreetest.mp3#fragment',
'https://archive.org/download/testmp3testfile/mpthreetest.mp3?foo=bar&id=1',
'http://www.music.helsinki.fi/tmt/opetus/uusmedia/esim/a2002011001-e02.wav',
'http://www.music.helsinki.fi/tmt/opetus/uusmedia/esim/a2002011001-e02.wav#fragment',
'http://www.music.helsinki.fi/tmt/opetus/uusmedia/esim/a2002011001-e02.wav?foo=bar&id=4',
'https://www.w3schools.com/html/horse.ogg',
'https://www.w3schools.com/html/horse.ogg#fragment',
'https://www.w3schools.com/html/horse.ogg?foo=bar&id=31',
];
urls.forEach(function (url) {
var element = domElement('<a href="' + url + '">' + url + '</a>');
mediaEmbedder.replaceLinksWithEmbeds(element);
assert.equal(element.childElementCount, 1);
assert.equal(element.children[0].tagName, 'AUDIO');
assert.equal(element.children[0].src, url.toLowerCase());
});
});
it('does not replace links if the link text is different', function () { it('does not replace links if the link text is different', function () {
var url = 'https://youtu.be/QCkm0lL-6lc'; var url = 'https://youtu.be/QCkm0lL-6lc';
var element = domElement('<a href="' + url + '">different label</a>'); var element = domElement('<a href="' + url + '">different label</a>');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment