-
Notifications
You must be signed in to change notification settings - Fork 33
/
utilsFS.ts
123 lines (99 loc) · 3.05 KB
/
utilsFS.ts
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
import _, {Dictionary} from 'lodash';
import {readFileSync, statSync} from 'fs';
import {parse, resolve, join, sep} from 'path';
import liquid from './liquid';
import {StateCore} from './typings';
const filesCache: Record<string, string> = {};
export function isFileExists(file: string) {
try {
const stats = statSync(file);
return stats.isFile();
} catch (e) {
return false;
}
}
export function resolveRelativePath(fromPath: string, relativePath: string) {
const {dir: fromDir} = parse(fromPath);
return resolve(fromDir, relativePath);
}
export type GetFileTokensOpts = {
getVarsPerFile?: (path: string) => Record<string, string>;
vars?: Record<string, unknown>;
disableLiquid?: boolean;
disableLint?: boolean;
lintMarkdown?: (opts: {input: string; path: string; sourceMap?: Dictionary<string>}) => void;
disableTitleRefSubstitution?: boolean;
disableCircularError?: boolean;
inheritVars?: boolean;
};
export function getFileTokens(path: string, state: StateCore, options: GetFileTokensOpts) {
const {
getVarsPerFile,
vars,
disableLiquid,
disableLint,
lintMarkdown,
disableTitleRefSubstitution,
disableCircularError,
inheritVars = true,
} = options;
let content;
const builtVars = (getVarsPerFile && !inheritVars ? getVarsPerFile(path) : vars) || {};
if (filesCache[path]) {
content = filesCache[path];
} else {
content = readFileSync(path, 'utf8');
filesCache[path] = content;
}
let sourceMap;
if (!disableLiquid) {
const liquidResult = liquid(content, builtVars, path, {withSourceMap: true});
content = liquidResult.output;
sourceMap = liquidResult.sourceMap;
}
if (!disableLint && lintMarkdown) {
lintMarkdown({
input: content,
path,
sourceMap,
});
}
const meta = state.md.meta;
const tokens = state.md.parse(content, {
...state.env,
path,
disableTitleRefSubstitution,
disableCircularError,
});
state.md.meta = meta;
return tokens;
}
export const getFullIncludePath = (includePath: string, root: string, path: string) => {
let fullIncludePath;
if (includePath.startsWith(sep)) {
fullIncludePath = join(root, includePath);
} else {
fullIncludePath = resolveRelativePath(path, includePath);
}
return fullIncludePath;
};
export function getSinglePageAnchorId(args: {
root: string;
currentPath: string;
pathname?: string;
hash?: string | null;
}) {
const {root, currentPath, pathname, hash} = args;
let resultAnchor = currentPath;
if (pathname) {
resultAnchor = resolveRelativePath(currentPath, pathname);
}
resultAnchor = resultAnchor
.replace(root, '')
.replace(/\.(md|ya?ml|html)$/i, '')
.replace(new RegExp(_.escapeRegExp(sep), 'gi'), '_');
if (hash) {
resultAnchor = resultAnchor + '_' + hash.slice(1);
}
return `#${resultAnchor}`;
}