-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtext-tools.js
82 lines (68 loc) · 2.35 KB
/
text-tools.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
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
// Три тире -- это горизонтальная полоса в Markdown. Пробелы между тире специально добавлены,
// чтобы markdown не воспринял случайно эти три подчёркивания как заголовок.
const DELIMITER = '- - -';
function dedent(text) {
let lines = text.split('\n');
let indents = [];
for (let line of lines){
if (!line.trim()){
continue;
}
let indentSize = line.length - line.trimLeft().length;
indents.push(indentSize);
}
if (!indents.length){
indents.push(0);
}
let minIndent = Math.min(...indents);
let formattedLines = lines.map(line => line.substring(minIndent));
return formattedLines.join('\n');
}
function trimRightEachLine(text) {
let lines = text.split('\n');
let formattedLines = lines.map(line => line.trimRight());
return formattedLines.join('\n');
}
function removeRedundantBlankLines(text){
return text.replace(/\n{2,}/g, '\n\n').replace(/^\n+/, '').replace(/\n+$/, '');
}
function addIndentBeforeOctothorpe(text){
// FIXME workaround for devman reviewer UI bug - add single whitespace before #
if (text.search(/^#/m) < 0){
return text;
}
return text.replace(/^/gm, ' '); // add whitespace to every line to not broke Python code
}
function prepareCodeSnippet(text){
let preparedText = trimRightEachLine(text);
preparedText = dedent(preparedText);
preparedText = removeRedundantBlankLines(preparedText);
preparedText = addIndentBeforeOctothorpe(preparedText);
return preparedText;
}
const FILES_TYPE = [
{ path: '.py', type: 'python' },
{ path: '.js', type: 'js' },
{ path: '.html', type: 'html' },
{ path: '.svg', type: 'markup' },
{ path: '.xml', type: 'markup' },
{ path: '.css', type: 'css' },
{ path: '.sh', type: 'bash' },
{ path: 'Dockerfile', type: 'dockerfile' },
{ path: '.diff', type: 'diff' },
{ path: '.json', type: 'json' },
{ path: '.md', type: 'md' },
{ path: '.yml', type: 'yaml' },
{ path: '.yaml', type: 'yaml' },
{ path: '.sql', type: 'sql' },
{ path: '.editorconfig', type: 'editorconfig' },
{ path: '.toml', type: 'toml' },
{ path: '.ini', type: 'ini' },
]
function detectSyntaxByFilename(filename){
const file = FILES_TYPE.find(fileType => filename.includes(fileType.path));
if (file) {
return file.type;
}
return '';
}