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
·138 lines (99 loc) · 2.91 KB
/
Copy pathloadSrcAsync.js
File metadata and controls
executable file
·138 lines (99 loc) · 2.91 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
'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 Plunk = require('plunk').Plunk;
const path = require('path');
const fs = require('mz/fs');
const t = require('i18n');
const tokenUtils = require('./utils/token');
var LANG = require('config').lang;
t.requirePhrase('markit.error', require('./locales/error/' + LANG + '.yml'));
class SrcError extends Error {
}
function srcUnderRoot(root, src) {
let absolutePath = path.join(root, src);
if (absolutePath.slice(0, root.length + 1) != root + '/') {
throw new SrcError(t('markit.error.src_outside_of_root', {src}));
}
return absolutePath;
}
module.exports = function* (tokens, options) {
let methods = {
blocktag_codetabs: src2plunk,
blocktag_edit: src2plunk,
blocktag_iframe,
blocktag_source,
link_open
};
function* src2plunk(token) {
let src = path.join(options.resourceWebRoot, token.blockTagAttrs.src);
let plunk = yield Plunk.findOne({webPath: src});
if (!plunk) {
throw new SrcError(t('markit.error.no_such_plunk', {src}));
}
token.plunk = plunk;
}
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 = yield Plunk.findOne({webPath: src});
if (!plunk) {
throw new SrcError(t('markit.error.no_such_plunk', {src: href}));
}
tokenUtils.attrReplace(token, 'href', plunk.getUrl());
}
function* blocktag_iframe(token) {
if (token.blockTagAttrs.edit || token.blockTagAttrs.zip) {
yield* src2plunk(token);
}
}
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 = yield 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;
}
function* walk(tokens, isInline) {
for (let idx = 0; idx < tokens.length; idx++) {
let token = tokens[idx];
let process = methods[token.type];
if (process) {
try {
yield* 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) {
yield* walk(token.children, true);
}
}
}
yield* walk(tokens);
};