方法一:
環境要求
- jekyll
- 七牛
開始使用
step1
首先下載這個文件open-embed.html,右鍵,另存爲Html
;下面是源碼,也可以複製保存下面的源碼;
<style>
.videoWrapper {
position: relative;
padding-bottom: 56.333%;
height: 0;
background: black;
}
.videoWrapper iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border: 0;
}
</style>
<script>
function get_youtube_id(url) {
var p = /^(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/;
return (url.match(p)) ? RegExp.$1 : false;
}
function vimeo_embed(url,el) {
var id = false;
$.ajax({
url: 'https://vimeo.com/api/oembed.json?url='+url,
async: true,
success: function(response) {
if(response.video_id) {
id = response.video_id;
if(url.indexOf('autoplay=1') !== -1) var autoplay=1; else var autoplay=0;
if(url.indexOf('loop=1') !== -1) var loop=1; else var loop=0;
var theInnerHTML = '<div class="videoWrapper"><iframe src="https://player.vimeo.com/video/'+id+'/?byline=0&title=0&portrait=0';
if(autoplay==1) theInnerHTML += '&autoplay=1';
if(loop==1) theInnerHTML += '&loop=1';
theInnerHTML += '" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></div>';
el.innerHTML = theInnerHTML;
}
}
});
}
function video_embed() {
var p = document.getElementsByTagName('p');
for(var i = 0; i < p.length; i++) {
//check if this is an external url (that starts with https:// or http://
if (p[i].innerHTML.indexOf("http://") == 0 ||
p[i].innerHTML.indexOf("https://") == 0) {
var youtube_id = get_youtube_id(p[i].innerHTML);
if(youtube_id) {
if(p[i].innerHTML.indexOf('autoplay=1') !== -1) var autoplay=1; else var autoplay=0;
if(p[i].innerHTML.indexOf('loop=1') !== -1) var loop=1; else var loop=0;
var theInnerHTML = '<div class="videoWrapper"><iframe width="720" height="420" src="https://www.youtube.com/embed/' + youtube_id + '?rel=0&showinfo=0';
if(autoplay==1) theInnerHTML += '&autoplay=1';
if(loop==1) theInnerHTML += '&loop=1&playlist='+youtube_id+'&version=3';
theInnerHTML += '" frameborder="0" allowfullscreen></iframe></div>';
p[i].innerHTML = theInnerHTML;
}
if(p[i].innerHTML.indexOf('vimeo.com') !== -1) {
//ask vimeo for the id and place the embed
vimeo_embed(p[i].innerHTML,p[i]);
}
}
}
}
video_embed();
function mp3_embed() {
var p = document.getElementsByTagName('p');
var song_format = ['.mp3','.m4a','.wma']; // 音樂格式
for(var i = 0; i < p.length; i++) {
for(var j=0;j<song_format.length;j++){
if(p[i].innerHTML.toLowerCase().indexOf(song_format[j]) !== -1) {
var str = p[i].innerHTML.split('?');
if(str.length == 1) str[1] = '';
var str1 = str[1];
str1 = str1.replace('&','').replace('&','');
str1 = str1.replace('autoplay=1','').replace('autoplay=0','');
str1 = str1.replace('loop=1','').replace('loop=0','');
str1 = str1.replace('controls=0','').replace('controls=1','');
if (str[0].toLowerCase().lastIndexOf(song_format[j], str[0].length - 4) === str[0].length - 4 && str1.length == 0) {
if(str[1].indexOf('autoplay=1') !== -1) var autoplay=1; else var autoplay=0;
if(str[1].indexOf('loop=1') !== -1) var loop=1; else var loop=0;
if(str[1].indexOf('controls=0') !== -1) var controls=0; else var controls=1;
var newInnerHTML = '<audio';
if(autoplay==1) newInnerHTML += ' autoplay';
if(loop==1) newInnerHTML += ' loop';
if(controls==1) newInnerHTML += ' controls';
newInnerHTML += '><source src="'+str[0]+'" type="audio/mpeg">Your browser does not support the audio element.</audio>';
p[i].innerHTML = newInnerHTML;
}
}
}
}
}
mp3_embed();
</script>
step2
將上一步存儲好的文件,保存到_includes
目錄下,就像下面這個樣子
step3
在_layouts
目錄下,default.html
這個文件,打開,在最下面,找到對應的標籤</body>
,加入如下代碼,注意:標籤不要重複了
{% include open-embed.html %}
使用語法
藉助七牛雲之類的圖牀或雲端存儲,將需要的音樂文件上傳至圖牀,並獲得外鏈。
然後,在Markdown中直接使用:
<p>音樂外鏈url</p>
注意:音樂鏈接url增加了支持MP3、wma、m4a格式
方法二:
添加網易雲音樂插件
- 在
include
目錄下創建cloud-music.html
,具體代碼如下:
<!-- cloud music -->
<!-- auto=1 可以控制自動播放與否,當值爲 1 即打開網頁就自動播放,值爲 0 時需要訪客手動點擊播放 -->
<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86
src="//music.163.com/outchain/player?type=2&id={{ page.music-id }}&auto=0&height=66">
</iframe>
- 將整個
cloud-music.html
嵌入post.html
,具體位置可以放在正文開頭:
<!-- 在正文開頭添加網易雲音樂插件 -->
{% if page.music-id %}
{% include cloud-music.html %}
{% endif %}
{{ content }}
在需要添加音樂的md文件開頭的配置項添加music-id: *****
,填寫網易雲生成外鏈播放器
的曲目具體id。
來源:https://blog.csdn.net/ds19991999/article/details/81293467