-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
compile.js
35 lines (27 loc) · 1 KB
/
compile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import Story from '../Story.js';
import StoryFormat from '../StoryFormat.js';
/**
* Write a combination of Story + StoryFormat into Twine 2 HTML file.
* @param {Story} story - Story object to write.
* @param {StoryFormat} storyFormat - StoryFormat to write.
* @returns {string} Twine 2 HTML.
*/
function compile (story, storyFormat) {
if (!(story instanceof Story)) {
throw new Error('Error: story must be a Story object!');
}
if (!(storyFormat instanceof StoryFormat)) {
throw new Error('storyFormat must be a StoryFormat object!');
}
let outputContents = '';
const storyData = story.toTwine2HTML();
// Replace the story name in the source file.
storyFormat.source = storyFormat.source.replaceAll(/{{STORY_NAME}}/gm, story.name);
// Replace the story data.
storyFormat.source = storyFormat.source.replaceAll(/{{STORY_DATA}}/gm, storyData);
// Combine everything together.
outputContents += storyFormat.source;
// Return content.
return outputContents;
}
export { compile };