forked from javascript-tutorial/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadSrcAsync.js
More file actions
executable file
·139 lines (101 loc) · 2.89 KB
/
loadSrcAsync.js
File metadata and controls
executable file
·139 lines (101 loc) · 2.89 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'use strict';
/**
* Loads info from external sources for
* sandbox: links
* codetabs
* edit
* iframe edit
* [js src...] (for editing)
*
* @type {ok|exports|module.exports}
*/
const assert = require('assert');
const path = require('path');
const fs = require('mz/fs');
const t = require('i18n');
const tokenUtils = require('./utils/token');
t.requirePhrase('markit', 'error');
class SrcError extends Error {
}
function srcUnderRoot(root, src) {
let absolutePath = path.join(root, src);
if (absolutePath.slice(0, root.length + 1) !== root + path.sep) {
throw new SrcError(t('markit.error.src_out_of_root', {src}));
}
return absolutePath;
}
let storage;
module.exports = async function (tokens, options) {
if (!storage) {
storage = require('tutorial').TutorialViewStorage.instance();
}
let methods = {
blocktag_codetabs: src2plunk,
blocktag_edit: src2plunk,
blocktag_iframe,
blocktag_source,
link_open
};
async function src2plunk(token) {
let src = path.join(options.resourceWebRoot, token.blockTagAttrs.src);
let plunk = storage.get(src);
if (!plunk) {
throw new SrcError(t('markit.error.no_such_plunk', {src}));
}
token.plunk = plunk;
}
async function link_open(token) {
let href = tokenUtils.attrGet(token, 'href');
if (!href.startsWith('sandbox:')) return;
let src = path.join(options.resourceWebRoot, href.slice('sandbox:'.length));
let plunk = storage.get(src);
if (!plunk) {
throw new SrcError(t('markit.error.no_such_plunk', {src: href}));
}
tokenUtils.attrReplace(token, 'href', plunk.getUrl());
}
async function blocktag_iframe(token) {
if (token.blockTagAttrs.edit || token.blockTagAttrs.zip) {
await src2plunk(token);
}
}
async function blocktag_source(token) {
if (!token.blockTagAttrs.src) return;
let sourcePath = srcUnderRoot(
options.publicRoot,
path.join(options.resourceWebRoot, token.blockTagAttrs.src)
);
let content;
try {
content = await fs.readFile(sourcePath, 'utf-8');
} catch (e) {
throw new SrcError(
t('markit.error.read_file', {src: token.blockTagAttrs.src}) +
(process.env.NODE_ENV == 'development' ? ` [${sourcePath}]` : '')
);
}
token.content = content;
}
async function walk(tokens, isInline) {
for (let idx = 0; idx < tokens.length; idx++) {
let token = tokens[idx];
let process = methods[token.type];
if (process) {
try {
await process(token);
} catch (err) {
if (err instanceof SrcError) {
token.type = isInline ? 'markdown_error_inline' : 'markdown_error_block';
token.content = err.message;
} else {
throw err;
}
}
}
if (token.children) {
await walk(token.children, true);
}
}
}
await walk(tokens);
};