Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 15 additions & 18 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -475,24 +475,6 @@ math:
copy_tex_js: //cdn.jsdelivr.net/npm/katex@0/dist/contrib/copy-tex.min.js
copy_tex_css: //cdn.jsdelivr.net/npm/katex@0/dist/contrib/copy-tex.min.css

# PDF Support
# Dependencies: https://github.com/theme-next/theme-next-pdf
pdf:
enable: false

# Default (true) will load PDFObject / PDF.js script on demand.
# That is it only render those page which has `pdf: true` in Front Matter.
# If you set it to false, it will load PDFObject / PDF.js srcipt EVERY PAGE.
per_page: true

height: 500px

pdfobject:
# Use 2.1.1 as default, jsdelivr as default CDN, works everywhere even in China
cdn: //cdn.jsdelivr.net/npm/pdfobject@2.1.1/pdfobject.min.js
# CDNJS, provided by cloudflare, maybe the best CDN, but not works in China
#cdn: //cdnjs.cloudflare.com/ajax/libs/pdfobject/2.1.1/pdfobject.min.js

# Han Support
# Dependencies: https://github.com/theme-next/theme-next-han
han: false
Expand Down Expand Up @@ -807,6 +789,21 @@ tabs:
labels: true
border_radius: 0

# PDF tag, requires two plugins: pdfObject and pdf.js
# pdfObject will try to load pdf files natively, if failed, pdf.js will be used.
# The following `cdn` setting is only for pdfObject, because cdn for pdf.js might be blocked by CORS policy.
# So, YOU MUST install the dependency of pdf.js if you want to use pdf tag and make it work on all browsers.
# See: https://github.com/theme-next/theme-next-pdf
pdf:
enable: false
# Default height
height: 500px
pdfobject:
# Use 2.1.1 as default, jsdelivr as default CDN, works everywhere even in China
cdn: //cdn.jsdelivr.net/npm/pdfobject@2.1.1/pdfobject.min.js
# CDNJS, provided by cloudflare, maybe the best CDN, but not works in China
#cdn: //cdnjs.cloudflare.com/ajax/libs/pdfobject/2.1.1/pdfobject.min.js


#! ---------------------------------------------------------------
#! DO NOT EDIT THE FOLLOWING SETTINGS
Expand Down
76 changes: 32 additions & 44 deletions layout/_third-party/pdf.swig
Original file line number Diff line number Diff line change
@@ -1,46 +1,34 @@
{% if theme.pdf.enable %}
{% set is_index_has_pdf = false %}

{# At home, check if there has `pdf: true` post #}
{% if is_home() %}
{% for post in page.posts %}
{% if post.pdf and not is_index_has_pdf %}
{% set is_index_has_pdf = true %}
{% endif %}
{% endfor %}
{% endif %}

{% if not theme.pdf.per_page or (is_index_has_pdf or page.pdf) %}
<script src="{{ theme.pdf.pdfobject.cdn }}"></script>
<script>
var options = {
pdfOpenParams: {
navpanes: 0,
toolbar: 0,
statusbar: 0,
pagemode: "thumbs",
view: "FitV"
},
PDFJS_URL: "/lib/pdf/web/viewer.html",
height: "{{ theme.pdf.height }}"
};
$("#main a").each(function() {
var href = $(this).attr("href");
if (!href) return;
if (href.endsWith(".pdf") || href.endsWith(".pdf/")) {
var id = "pdf" + Math.floor(Math.random() * 10000);
$(this).after(`<div id="${id}"></div>`).remove();
PDFObject.embed(href, "#" + id, options);
}
});
</script>
<style>
.pdfobject-container {
position: relative;
overflow: auto;
width: 100%;
height: {{ theme.pdf.height }};
}
</style>
{% endif %}
<style>
.pdfobject-container {
position: relative;
overflow: auto;
width: 100%;
}
</style>
<script>
if ($('body').find('div.pdf').length) {
$.ajax({
type: 'GET',
url: '{{ theme.pdf.pdfobject.cdn }}',
dataType: 'script',
cache: true,
success: function() {
$('body').find('div.pdf').each(function(i, o) {
PDFObject.embed($(o).attr('target'), $(o), {
pdfOpenParams: {
navpanes: 0,
toolbar: 0,
statusbar: 0,
pagemode: 'thumbs',
view: 'FitH'
},
PDFJS_URL: '/lib/pdf/web/viewer.html',
height: $(o).attr('height') || '{{ theme.pdf.height }}'
});
});
},
});
}
</script>
{% endif %}
13 changes: 13 additions & 0 deletions scripts/tags/pdf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* pdf.js | https://theme-next.org/docs/tag-plugins/pdf/
*/

/* global hexo */

'use strict';

function pdf(args) {
return `<div class="pdf" target="${args[0]}" height="${args[1] || ''}"></div>`;
}

hexo.extend.tag.register('pdf', pdf, {ends: false});