Skip to content

Commit bac2b1c

Browse files
authored
Merge pull request #115 from easycoder/dev
Showdown extension
2 parents 5483598 + 184f862 commit bac2b1c

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

iwsy/iwsy.js

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ const IWSY = (player, text) => {
203203
}
204204
for (const text of script.content) {
205205
if (text.name === item.content) {
206-
const converter = new showdown.Converter();
206+
const converter = new showdown.Converter({
207+
extensions: [`Extension`]
208+
});
207209
block.textPanel.innerHTML =
208210
converter.makeHtml(text.content.split(`%0a`).join(`\n`));
209211
break;
@@ -304,7 +306,9 @@ const IWSY = (player, text) => {
304306
const crossfade = step => {
305307
for (const content of script.content) {
306308
if (content.name === step.target) {
307-
const converter = new showdown.Converter();
309+
const converter = new showdown.Converter({
310+
extensions: [`Extension`]
311+
});
308312
const newText = converter.makeHtml(content.content.split(`%0a`).join(`\n`));
309313
for (const block of script.blocks) {
310314
if (block.defaults.name === step.block) {
@@ -725,12 +729,66 @@ const IWSY = (player, text) => {
725729
if (typeof showdown === `undefined`) {
726730
require(`js`, `https://cdn.rawgit.com/showdownjs/showdown/1.9.1/dist/showdown.min.js`,
727731
() => {
732+
showdown.extension(`Extension`, {
733+
type: `lang`,
734+
filter: function (text, converter) {
735+
return text.replace(/~([^~]+)~/g, function (match, group) {
736+
return decodeShowdown(group);
737+
});
738+
}
739+
});
728740
});
729741
}
730742
else {
731743
}
732744
};
733745

746+
// Decode special Showdown tags
747+
const decodeShowdown = group => {
748+
if (group.slice(0, 5) === `code:`) {
749+
return `<span style="font-family:mono;color:darkred">${group.slice(5)}</span>`;
750+
}
751+
if (group.slice(0, 5) === `html:`) {
752+
return group.slice(5);
753+
}
754+
if (group.slice(0, 4) === `img:`) {
755+
const data = group.slice(4);
756+
const colon = data.indexOf(`:`);
757+
if (colon > 0) {
758+
const src = data.slice(0, colon);
759+
const classes = data.slice(colon + 1).split(` `);
760+
const styles = [];
761+
for (const item of classes) {
762+
if (item.endsWith(`%`)) {
763+
styles.push(`width:${item}`);
764+
} else if (item.startsWith(`{`) && item.endsWith(`}`)) {
765+
styles.push(item.slice(1, -1));
766+
} else {
767+
switch (item) {
768+
case `left`:
769+
styles.push(`float:left`);
770+
break;
771+
case `center`:
772+
styles.push(`margin:0 auto`);
773+
break;
774+
case `right`:
775+
styles.push(`float:right`);
776+
break;
777+
case `clear`:
778+
styles.push(`clear:both`);
779+
break;
780+
case `border`:
781+
styles.push(`padding:2px;border:1px solid black`);
782+
break;
783+
}
784+
}
785+
}
786+
return `<img src="${src}" style="${styles.join(`;`)}" />`;
787+
}
788+
}
789+
return group;
790+
};
791+
734792
// Load a JS or CSS library
735793
const require = (type, src, cb) => {
736794
let prefix = ``;

iwsy/resources/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# JSON::Presenter resources
1+
# IWSY resources
22

33
These are all the resources - scripts, images and text files - called for by the website.

0 commit comments

Comments
 (0)