Add support of embed pdf

This commit is contained in:
Cheng-Han, Wu 2016-06-21 21:44:06 +08:00
parent 558304ff62
commit 3424cdd771
6 changed files with 52 additions and 16 deletions

View File

@ -55,7 +55,8 @@
"list.js": "^1.2.0", "list.js": "^1.2.0",
"store-js": "store.js#^1.3.20", "store-js": "store.js#^1.3.20",
"string": "^3.3.1", "string": "^3.3.1",
"visibilityjs": "^1.2.3" "visibilityjs": "^1.2.3",
"pdfobject": "pdfobject2#*"
}, },
"resolutions": { "resolutions": {
"jquery": "~2.2.4" "jquery": "~2.2.4"

View File

@ -186,6 +186,10 @@ alert(s);
### Speakerdeck ### Speakerdeck
{%speakerdeck sugarenia/xxlcss-how-to-scale-css-and-keep-your-sanity %} {%speakerdeck sugarenia/xxlcss-how-to-scale-css-and-keep-your-sanity %}
### PDF
**Caution: this might be blocked by your browser if not using https url.**
{%pdf https://papers.nips.cc/paper/5346-sequence-to-sequence-learning-with-neural-networks.pdf %}
## MathJax ## MathJax
You can render *LaTeX* mathematical expressions using **MathJax**, as on [math.stackexchange.com](http://math.stackexchange.com/): You can render *LaTeX* mathematical expressions using **MathJax**, as on [math.stackexchange.com](http://math.stackexchange.com/):

View File

@ -83,6 +83,20 @@ function slugifyWithUTF8(text) {
return newText; return newText;
} }
function isValidURL(str) {
var pattern = new RegExp('^(https?:\\/\\/)?' + // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path
'(\\?[;&a-z\\d%_.~+=-]*)?' + // query string
'(\\#[-a-z\\d_]*)?$', 'i'); // fragment locator
if (!pattern.test(str)) {
return false;
} else {
return true;
}
}
//parse meta //parse meta
function parseMeta(md, edit, view, toc, tocAffix) { function parseMeta(md, edit, view, toc, tocAffix) {
var lang = null; var lang = null;
@ -403,6 +417,16 @@ function finishView(view) {
} }
}); });
}); });
//pdf
view.find(".pdf.raw").removeClass("raw")
.each(function (key, value) {
var url = $(value).attr('data-pdfurl');
var inner = $('<div></div>');
$(this).append(inner);
PDFObject.embed(url, inner, {
height: '400px'
});
});
//render title //render title
document.title = renderTitle(view); document.title = renderTitle(view);
} }
@ -901,6 +925,20 @@ var speakerdeckPlugin = new Plugin(
return div[0].outerHTML; return div[0].outerHTML;
} }
); );
//pdf
var pdfPlugin = new Plugin(
// regexp to match
/{%pdf\s*([\d\D]*?)\s*%}/,
// this function will be called when something matches
function (match, utils) {
var pdfurl = match[1];
if (!isValidURL(pdfurl)) return match[0];
var div = $('<div class="pdf raw"></div>');
div.attr('data-pdfurl', pdfurl);
return div[0].outerHTML;
}
);
//yaml meta, from https://github.com/eugeneware/remarkable-meta //yaml meta, from https://github.com/eugeneware/remarkable-meta
function get(state, line) { function get(state, line) {
@ -950,3 +988,4 @@ md.use(gistPlugin);
md.use(tocPlugin); md.use(tocPlugin);
md.use(slidesharePlugin); md.use(slidesharePlugin);
md.use(speakerdeckPlugin); md.use(speakerdeckPlugin);
md.use(pdfPlugin);

View File

@ -152,6 +152,10 @@ var supportExternals = [
{ {
text: '{%speakerdeck speakerdeckid %}', text: '{%speakerdeck speakerdeckid %}',
search: 'speakerdeck' search: 'speakerdeck'
},
{
text: '{%pdf pdfurl %}',
search: 'pdf'
} }
]; ];
var supportExtraTags = [ var supportExtraTags = [
@ -1836,20 +1840,6 @@ function importFromUrl(url) {
}); });
} }
function isValidURL(str) {
var pattern = new RegExp('^(https?:\\/\\/)?' + // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path
'(\\?[;&a-z\\d%_.~+=-]*)?' + // query string
'(\\#[-a-z\\d_]*)?$', 'i'); // fragment locator
if (!pattern.test(str)) {
return false;
} else {
return true;
}
}
//mode //mode
ui.toolbar.mode.click(function () { ui.toolbar.mode.click(function () {
toggleMode(); toggleMode();

View File

@ -59,6 +59,7 @@
<script src="<%- url %>/vendor/sequence-diagrams/sequence-diagram-min.js" defer></script> <script src="<%- url %>/vendor/sequence-diagrams/sequence-diagram-min.js" defer></script>
<script src="<%- url %>/vendor/flowchart/release/flowchart.min.js" defer></script> <script src="<%- url %>/vendor/flowchart/release/flowchart.min.js" defer></script>
<script src="<%- url %>/vendor/viz.js/viz.js" defer></script> <script src="<%- url %>/vendor/viz.js/viz.js" defer></script>
<script src="<%- url %>/vendor/pdfobject/pdfobject.min.js" defer></script>
<script src="<%- url %>/vendor/file-saver/FileSaver.min.js" defer></script> <script src="<%- url %>/vendor/file-saver/FileSaver.min.js" defer></script>
<script src="<%- url %>/vendor/store-js/store.min.js" defer></script> <script src="<%- url %>/vendor/store-js/store.min.js" defer></script>
<script src="<%- url %>/vendor/js-url/url.min.js" defer></script> <script src="<%- url %>/vendor/js-url/url.min.js" defer></script>

View File

@ -116,6 +116,7 @@
<script src="<%- url %>/vendor/sequence-diagrams/sequence-diagram-min.js" defer></script> <script src="<%- url %>/vendor/sequence-diagrams/sequence-diagram-min.js" defer></script>
<script src="<%- url %>/vendor/flowchart/release/flowchart.min.js" defer></script> <script src="<%- url %>/vendor/flowchart/release/flowchart.min.js" defer></script>
<script src="<%- url %>/vendor/viz.js/viz.js" defer></script> <script src="<%- url %>/vendor/viz.js/viz.js" defer></script>
<script src="<%- url %>/vendor/pdfobject/pdfobject.min.js" defer></script>
<script src="<%- url %>/vendor/md-toc.js" defer></script> <script src="<%- url %>/vendor/md-toc.js" defer></script>
<script src="<%- url %>/js/common.js" defer></script> <script src="<%- url %>/js/common.js" defer></script>
<script src="<%- url %>/js/extra.js" defer></script> <script src="<%- url %>/js/extra.js" defer></script>