Skip to content

Commit

Permalink
Merge pull request theme-next#466 from stevenjoezhang/pdfjs
Browse files Browse the repository at this point in the history
New Feature: PDFObject and PDF.js support
  • Loading branch information
ivan-nginx committed Nov 7, 2018
2 parents 2f50d75 + 9aaa14a commit 1864447
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
15 changes: 15 additions & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,21 @@ math:
# CDNJS, provided by cloudflare, maybe the best CDN, but not works in China
#cdn: //cdnjs.cloudflare.com/ajax/libs/KaTeX/0.7.1/katex.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 who has 'pdf: true' in Front Matter.
# If you set it to false, it will load PDFObject/PDF.js srcipt EVERY PAGE.
per_page: true

pdfobject:
# Use 2.1.1 as default, cloudflare as default CDN
cdn: //cdnjs.cloudflare.com/ajax/libs/pdfobject/2.1.1/pdfobject.min.js
height: 500px

# Han Support
# Dependencies: https://github.com/theme-next/theme-next-han
han: false
Expand Down
1 change: 1 addition & 0 deletions layout/_layout.swig
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,6 @@
{% include '_third-party/exturl.swig' %}
{% include '_third-party/bookmark.swig' %}
{% include '_third-party/copy-code.swig' %}
{% include '_third-party/pdf.swig' %}
</body>
</html>
46 changes: 46 additions & 0 deletions layout/_third-party/pdf.swig
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{% 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 type="text/javascript" src="{{ theme.pdf.pdfobject.cdn }}"></script>
<script type="text/javascript">
var options = {
pdfOpenParams: {
navpanes: 0,
toolbar: 0,
statusbar: 0,
pagemode: "thumbs",
view: "FitV"
},
PDFJS_URL: "/lib/pdf/web/viewer.html",
height: "{{ theme.pdf.pdfobject.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.pdfobject.height }};
}
</style>
{% endif %}
{% endif %}

0 comments on commit 1864447

Please sign in to comment.