Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle custom init in ESM format #1083

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ enableSearch: false
---
```

## Custom initialization (advanced)

You can override revealjs initialization by putting `init.js` (or `init.esm.js` if you prefer ES module format). This gives you total control over executed code, but you'll have to call [Reveal.initialize()](https://revealjs.com/initialization/]) yourself. You also won't have access to frontmatter configuration, defaults, etc. Use at your own risk.

## <a id="faq"></a> FAQ


Expand Down
13 changes: 10 additions & 3 deletions src/RevealServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,20 @@ export class RevealServer extends Disposable {
next()
}
else {

let init: string | null = null;
let init: string | null = null
let initType: string | null = null
if (context.dirname) {
const initPath = path.join(context.dirname, 'init.js')
let initPath = path.join(context.dirname, 'init.js')
if (fs.existsSync(initPath)) {
init = fs.readFileSync(initPath, "utf8");
init = fs.readFileSync(initPath, 'utf8')
initType = 'application/javascript'
}

initPath = path.join(context.dirname, 'init.esm.js')
if (!init && fs.existsSync(initPath)) {
init = fs.readFileSync(initPath, 'utf8')
initType = 'module'
}

const htmlSlides = context.slides.map((s) => ({
Expand Down
32 changes: 16 additions & 16 deletions views/init.ejs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% if(init) { %>
<script>
<script type="<%- initType %>">
<%- init %>
</script>
<%}else{ %>
Expand All @@ -18,7 +18,7 @@
const plugins = [...printPlugins,
<% if(enableZoom) {%>RevealZoom, <%}%>
<% if(enableSearch) {%>RevealSearch, <%}%>
RevealMarkdown,
RevealMarkdown,
<% if(enableMenu) {%>RevealMenu, <%}%>
RevealFullscreen,
RevealAnything,
Expand All @@ -28,7 +28,7 @@
// poll
// question
// seminar
Verticator
Verticator
]


Expand Down Expand Up @@ -148,33 +148,33 @@
]
},
chart: {
defaults: {
defaults: {
color: 'lightgray', // color of labels
scale: {
beginAtZero: true,
scale: {
beginAtZero: true,
ticks: { stepSize: 1 },
grid: { color: "lightgray" } , // color of grid lines
},
},
line: { borderColor: [ "rgba(20,220,220,.8)" , "rgba(220,120,120,.8)", "rgba(20,120,220,.8)" ], "borderDash": [ [5,10], [0,0] ] },
bar: { backgroundColor: [ "rgba(20,220,220,.8)" , "rgba(220,120,120,.8)", "rgba(20,120,220,.8)" ]},
line: { borderColor: [ "rgba(20,220,220,.8)" , "rgba(220,120,120,.8)", "rgba(20,120,220,.8)" ], "borderDash": [ [5,10], [0,0] ] },
bar: { backgroundColor: [ "rgba(20,220,220,.8)" , "rgba(220,120,120,.8)", "rgba(20,120,220,.8)" ]},
pie: { backgroundColor: [ ["rgba(0,0,0,.8)" , "rgba(220,20,20,.8)", "rgba(20,220,20,.8)", "rgba(220,220,20,.8)", "rgba(20,20,220,.8)"] ]},
radar: { borderColor: [ "rgba(20,220,220,.8)" , "rgba(220,120,120,.8)", "rgba(20,120,220,.8)" ]},
radar: { borderColor: [ "rgba(20,220,220,.8)" , "rgba(220,120,120,.8)", "rgba(20,120,220,.8)" ]},
},
math: {
mathjax: 'https://cdn.jsdelivr.net/gh/mathjax/[email protected]/MathJax.js',
config: 'TeX-AMS_HTML-full',
// pass other options into `MathJax.Hub.Config()`
TeX: { Macros: { RR: "{\\bf R}" } }
},
anything: [
anything: [
{
className: "plot",
defaults: {width:500, height: 500, grid:true},
initialize: (function(container, options){ options.target = "#"+container.id; functionPlot(options) })
},
{
className: "chart",
className: "chart",
initialize: (function(container, options){ container.chart = new Chart(container.getContext("2d"), options); })
},
{
Expand All @@ -183,12 +183,12 @@
},
],
// Learn about plugins: https://revealjs.com/plugins/
plugins: (window.location.search.match(/print-pdf/gi) ? printPlugins : plugins )
plugins: (window.location.search.match(/print-pdf/gi) ? printPlugins : plugins )
});



// Change chalkboard theme :

// Change chalkboard theme :
function changeTheme(input) {
var config = {};
config.theme = input.value;
Expand All @@ -211,7 +211,7 @@
setTimeout(() => {
window.print();
}, 2500);

}
</script>
<%}%>
<%}%>