Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
dcd1b2ca
Commit
dcd1b2ca
authored
Jul 21, 2017
by
Steel Wagstaff
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
mp3 -> html5 + H5P embed support
Added support for mp3 -> html5 audio and a primitive h5p embedder.
parent
e29a73f0
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
58 additions
and
7 deletions
+58
-7
media-embedder.js
src/sidebar/media-embedder.js
+31
-6
media-embedder-test.js
src/sidebar/test/media-embedder-test.js
+27
-1
No files found.
src/sidebar/media-embedder.js
View file @
dcd1b2ca
'use strict'
;
'use strict'
;
/**
* Return an HTML5 audio player with the given src URL.
*/
function
mp3audio
(
src
)
{
var
html5audio
=
document
.
createElement
(
'audio'
);
html5audio
.
id
=
'audio-player'
;
html5audio
.
controls
=
'controls'
;
html5audio
.
src
=
src
;
html5audio
.
type
=
'audio/mpeg'
;
return
html5audio
;
}
/**
/**
* Return an iframe DOM element with the given src URL.
* Return an iframe DOM element with the given src URL.
*/
*/
...
@@ -24,8 +37,8 @@ function vimeoEmbed(id) {
...
@@ -24,8 +37,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 +101,20 @@ var embedGenerators = [
...
@@ -88,6 +101,20 @@ var embedGenerators = [
}
}
return
null
;
return
null
;
},
},
function
html5audioFromMp3Link
(
link
)
{
if
(
link
.
href
.
toLowerCase
().
indexOf
(
'.mp3'
)
!==
-
1
)
{
return
mp3audio
(
link
.
href
);
}
return
null
;
},
function
h5pFromEmbedLink
(
link
)
{
if
(
link
.
href
.
toLowerCase
().
indexOf
(
'admin-ajax.php?action=h5p_embed&id='
)
!==
-
1
)
{
return
iframe
(
link
.
href
);
}
return
null
;
}
];
];
/**
/**
...
@@ -115,7 +142,7 @@ function embedForLink(link) {
...
@@ -115,7 +142,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 +165,12 @@ function replaceLinkWithEmbed(link) {
...
@@ -138,14 +165,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.
*
*
...
...
src/sidebar/test/media-embedder-test.js
View file @
dcd1b2ca
...
@@ -90,6 +90,32 @@ describe('media-embedder', function () {
...
@@ -90,6 +90,32 @@ describe('media-embedder', function () {
});
});
});
});
it
(
'replaces mp3 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?q=foo&id=bar'
,
];
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
,
'https://archive.org/download/testmp3testfile/mpthreetest.mp3'
);
//not sure if this is correct
});
});
})
/** it('replaces H5P activity links with iframes', function(){
* var urls = [
* 'https://h5p.org/h5p/embed/617'
* ]
* }
*/
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>'
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment