-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack-viewer.html
237 lines (204 loc) · 12.2 KB
/
stack-viewer.html
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script src="https://cdn.rawgit.com/showdownjs/showdown/1.8.6/dist/showdown.min.js"></script>
</head>
<body>
<div id="app"></div>
<script>
var vue = new Vue({
el: "#app",
template: `
<div id="app" class="row">
<div v-for="layer in layers" class="column" :ref="layer" :id="layer" v-html="html[layer]">
</div>
</div>
`,
data() {
const layers = ["digested", "original"];
const data = {
concepts: new Set(),
layers
};
layers.forEach(layer => data[layer] = null);
return data;
},
computed: {
html() {
if (this.layers.find(layer => !this[layer])) {
return {};
} else {
Vue.nextTick()
.then(this.handleHtmlLoaded);
return this.layers.reduce((acc, layer) => {
acc[layer] = this.markdownToHtml(layer);
return acc;
}, {});
}
},
converter() {
const converter = new showdown.Converter({
noHeaderId: true
});
const correlateExtension = {
type: 'output',
filter: (text, _, options) => {
const versionName = options.versionName;
const invisibleConceptBreakRegexp = /<p>\^% (.*)<\/p>/g;
const visibleConceptBreakRegexp = /<([a-z0-9]+)>(.*)\^% (.*)<\/\1>/g;
const headerRegexp = /<(h[1-7])>(.*)<\/\1>/g;
const getDataAttribute = str => str.trim().toLowerCase().replace(/[^a-zA-Z0-9]/g, "").replace(" ", "-");
const getId = str => `${versionName}-${str}`;
return text.split('\n').map(line => {
let match;
if ((match = invisibleConceptBreakRegexp.exec(line))) {
const innerText = match[1];
const dataAttribute = getDataAttribute(innerText);
this.concepts.add(dataAttribute);
const id = getId(dataAttribute);
return `
<span class="concept-break concept-break-invisible" data-concept="${dataAttribute}" id="${id}"></span>
`;
} else if ((match = visibleConceptBreakRegexp.exec(line))) {
const conceptText = match[3];
const dataAttribute = getDataAttribute(conceptText);
this.concepts.add(dataAttribute);
const id = getId(dataAttribute);
const tagName = match[1];
const innerText = match[2];
return `
<${tagName} class="concept-break" data-concept="${dataAttribute}" id="${id}">${innerText}</${tagName}>
`;
} else if ((match = headerRegexp.exec(line))) {
const innerText = match[2];
const dataAttribute = getDataAttribute(innerText);
this.concepts.add(dataAttribute);
const id = getId(dataAttribute);
const tagName = match[1];
return `
<${tagName} class="concept-break" data-concept="${dataAttribute}" id="${id}">${innerText}</${tagName}>
`;
} else {
return line;
}
}).join("\n");
}
};
showdown.extension('correlate', correlateExtension);
converter.addExtension('correlate');
return converter;
}
},
methods: {
markdownToHtml(versionName) {
const markdown = this[versionName];
this.converter.setOption('versionName', versionName);
return this.converter.makeHtml(markdown);
},
handleHtmlLoaded() {
const shuffledColours = _shuffle(_availableColours);
const colorCodedConcepts = Array.from(this.concepts).reduce((acc, concept, index) => {
acc[concept] = shuffledColours[index % shuffledColours.length];
return acc;
}, {});
// POC: connect concept breaks between versions using SVG lines
const domElementsByConcept = Array.from(this.concepts).reduce((acc, concept) => {
const domElementsByLayer = this.layers.reduce((acc, layer) => {
const element = this.$refs[layer][0].querySelector(`[data-concept='${concept}'`);
if (Array.from(element.classList).includes("concept-break-invisible")) {
acc[layer] = element.nextElementSibling;
} else {
acc[layer] = element;
}
return acc;
}, {});
acc[concept] = domElementsByLayer;
return acc;
}, {});
const textToDomNode = str => {
var template = document.createElement('template');
template.innerHTML = str.trim();
return template.content.firstChild;
}
const getHorizontalLineSvgNode = (domLeft, domRight, colour) => {
const rectLeft = domLeft.getBoundingClientRect();
const rectRight = domRight.getBoundingClientRect();
const x1 = rectLeft.x + rectLeft.width - window.scrollX;
const y1 = rectLeft.y + (rectLeft.height / 2) - window.scrollY;
const x2 = rectRight.x - window.scrollX;
const y2 = rectRight.y + (rectRight.height / 2) - window.scrollY;
return _makeSvg("line", {
x1, y1, x2, y2, stroke: colour
});
};
const svgNode = textToDomNode(`<svg viewBox="0 0 ${document.body.scrollWidth} ${document.body.scrollHeight}" style="position: absolute; top: 0; left: 0;" xmlns="http://www.w3.org/2000/svg">
</svg>`);
for (let [concept, domElementsByLayer] of Object.entries(domElementsByConcept)) {
const colour = colorCodedConcepts[concept];
const lineNodes = new Array(this.layers.length - 1).fill(0).map((_, i) =>
getHorizontalLineSvgNode(domElementsByLayer[this.layers[i]], domElementsByLayer[this.layers[i + 1]], colour)
);
lineNodes.forEach(n => svgNode.appendChild(n));
}
document.body.appendChild(svgNode);
}
},
mounted() {
const fetchVersion = str => {
const myRequest = new Request(str + '.md');
fetch(myRequest).then((response) => response
.text()
.then(text => {
this[str] = text;
}))
}
fetchVersion('original');
fetchVersion('digested');
}
});
</script>
<style>
body {
font-family: sans-serif;
}
code {
display: none;
}
.column {
width: 50%;
float: left;
}
.column :-webkit-any(h1,h2,h3,h4,h5,h6,h7,p) {
max-width: 100%;
width: fit-content;
}
</style>
<script>
function _shuffle(array) {
let currentIndex = array.length;
while (0 !== currentIndex) {
let randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
let temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
const _colourCodes = ["#F0F8FF","#FAEBD7","#00FFFF","#7FFFD4","#F0FFFF","#F5F5DC","#FFE4C4","#000000","#FFEBCD","#0000FF","#8A2BE2","#A52A2A","#DEB887","#5F9EA0","#7FFF00","#D2691E","#FF7F50","#6495ED","#FFF8DC","#DC143C","#00FFFF","#00008B","#008B8B","#B8860B","#A9A9A9","#006400","#BDB76B","#8B008B","#556B2F","#FF8C00","#9932CC","#8B0000","#E9967A","#8FBC8F","#483D8B","#2F4F4F","#00CED1","#9400D3","#FF1493","#00BFFF","#696969","#1E90FF","#B22222","#FFFAF0","#228B22","#FF00FF","#DCDCDC","#F8F8FF","#FFD700","#DAA520","#808080","#008000","#ADFF2F","#F0FFF0","#FF69B4","#CD5C5C","#4B0082","#FFFFF0","#F0E68C","#E6E6FA","#FFF0F5","#7CFC00","#FFFACD","#ADD8E6","#F08080","#E0FFFF","#FAFAD2","#90EE90","#D3D3D3","#FFB6C1","#FFA07A","#20B2AA","#87CEFA","#778899","#B0C4DE","#FFFFE0","#00FF00","#32CD32","#FAF0E6","#FF00FF","#800000","#66CDAA","#0000CD","#BA55D3","#9370D8","#3CB371","#7B68EE","#00FA9A","#48D1CC","#C71585","#191970","#F5FFFA","#FFE4E1","#FFE4B5","#FFDEAD","#000080","#FDF5E6","#808000","#688E23","#FFA500","#FF4500","#DA70D6","#EEE8AA","#98FB98","#AFEEEE","#D87093","#FFEFD5","#FFDAB9","#CD853F","#FFC0CB","#DDA0DD","#B0E0E6","#800080","#FF0000","#BC8F8F","#4169E1","#8B4513","#FA8072","#F4A460","#2E8B57","#FFF5EE","#A0522D","#C0C0C0","#87CEEB","#6A5ACD","#708090","#FFFAFA","#00FF7F","#4682B4","#D2B48C","#008080","#D8BFD8","#FF6347","#40E0D0","#EE82EE","#F5DEB3","#FFFFFF","#F5F5F5","#FFFF00","#9ACD32"];
const getRgb = str => Array(3).fill(0).map((_, index) => parseInt(str.slice(1 + index * 2, 3 + index * 2), 16));
const perceivedBrightness = rgb => Math.sqrt( [.241, .691, .068].reduce((acc, next, index) => acc + next * Math.pow(rgb[index], 2), 0))
const brightnessGateFilter = str => {
const brightness = perceivedBrightness(getRgb(str));
return brightness < 200 && brightness > 150;
}
const _availableColours = _colourCodes.filter(brightnessGateFilter);
function _makeSvg(tag, attrs) {
const el = document.createElementNS('http://www.w3.org/2000/svg', tag);
for (let [k, v] of Object.entries(attrs))
el.setAttribute(k, v);
return el;
}
</script>
</body>
</html>