-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGitHubToCBuilder.user.js
138 lines (126 loc) · 4.57 KB
/
GitHubToCBuilder.user.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
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
138
// ==UserScript==
// @name GitHubToCBuilder
// @namespace https://scand.com/
// @version 0.1.5
// @description ToC builder for GitHub markdown markup docs (.md and Wiki)
// @author vkuleshov-sc
// @author achernyakevich-sc
// @match https://github.com/*
// @grant GM_log
// @grant GM_registerMenuCommand
// @grant GM_setClipboard
// ==/UserScript==
(function () {
'use strict';
const getHeaderDepth = headerLine => {
return headerLine.match(/#+/) ? headerLine.match(/#+/)[0].length : 0;
}
const getHeaderText = headerLine => {
return headerLine.replace(/#+\s+/, '')
// For link in header we keep text only (remove URL and brackets)
.replace(/\[(.*?)\]\(.*?\)/g, '$1');
}
const getHeaderAnchor = headerText => {
return '#' + headerText.replace(/ /g, '-').replace(/\t/, '--').replace(/[^\d\w-_#]/g, '').toLowerCase();
}
const getHeaderLines = mdText => {
return mdText.split(/[\r\n]/).
filter(str => str.match(/^\s{0,3}#+\s+[^\r\n]*/g)).
map(str => str.trim());
}
const getToCForMarkdownMarkupText = mdText => {
let toc = '';
const headerLines = getHeaderLines(mdText);
if (headerLines) {
headerLines.forEach(line => {
const hDepth = getHeaderDepth(line);
const hText = getHeaderText(line);
const hAnchor = getHeaderAnchor(hText);
toc += `${' '.repeat((hDepth - 1) * 2)}- [${hText}](${hAnchor})\n`;
});
}
return toc;
}
const getWikiTextAreaElement = () => {
return document.getElementById('gollum-editor-body');
};
const copyToCForMarkdownMarkupTextToClipboard = () => {
const textArea = getWikiTextAreaElement();
if (textArea) {
GM_setClipboard(getToCForMarkdownMarkupText(textArea.value));
}
alert('ToC built from GitHub Wiki page content and copied to the clipboard!');
};
const copyToCForSelectedMarkdownMarkupTextToClipboard = () => {
const selectedText = document.getSelection().toString();
if (selectedText !== '') {
GM_setClipboard(getToCForMarkdownMarkupText(selectedText));
alert('ToC built from selected Markdown Markup and copied to the clipboard!');
} else {
alert('Nothing is selected!');
}
};
if (getWikiTextAreaElement()) {
GM_registerMenuCommand('Build ToC for Wiki content (editor->clipboard)', copyToCForMarkdownMarkupTextToClipboard);
} else {
GM_registerMenuCommand('Build ToC for selected Markdown Markup (selection->clipboard)', copyToCForSelectedMarkdownMarkupTextToClipboard);
}
// Tests - used only for development, can be commented out or deleted
(() => {
const test = ({ input, output, testingFunc }) => {
if (JSON.stringify(testingFunc(input)) !== JSON.stringify(output)) {
GM_log(`${testingFunc.name}(${JSON.stringify(input)}) !== ${JSON.stringify(output)}`);
GM_log(`${testingFunc.name}(${JSON.stringify(input)}) == ${JSON.stringify(testingFunc(input))}`);
alert('Test failed, see details in console');
}
};
const testCases = [
{
input: ' 123',
output: 0,
testingFunc: getHeaderDepth,
},
{
input: '######################### 123',
output: 25,
testingFunc: getHeaderDepth,
},
{
input: 'ab(c)?:;\'0!@$-%/\\1_^|23\tDEF',
output: '#abc0-1_23--def',
testingFunc: getHeaderAnchor,
},
{
input: '### header1',
output: 'header1',
testingFunc: getHeaderText,
},
{
input: '# header1 and some text',
output: 'header1 and some text',
testingFunc: getHeaderText,
},
{
input: '# Header with [GitHub](https://github.com/) and [Google](https://google.com/) links',
output: 'Header with GitHub and Google links',
testingFunc: getHeaderText,
},
{
input: `# header1\r\n### header2 some text\n## header3\r\n`,
output: ['# header1', '### header2 some text', '## header3'],
testingFunc: getHeaderLines,
},
{
input: `s # header1\r\n### header2 some text\n # not header1\n # header3 (some text)\n #not header2`,
output: ['### header2 some text', '# header3 (some text)'],
testingFunc: getHeaderLines,
},
{
input: `# header1\r\n### header2 some text\n## header3\r\n`,
output: '- [header1](#header1)\n - [header2 some text](#header2-some-text)\n - [header3](#header3)\n',
testingFunc: getToCForMarkdownMarkupText,
},
];
testCases.forEach(test);
})();
})();