Ver Fonte

Use linkExpander as a plugin, moving it to its own separate file

sbkwgh há 7 anos atrás
pai
commit
365065582c

+ 125 - 0
frontend/src/assets/js/linkExpander.js

@@ -0,0 +1,125 @@
+export default {
+	install (Vue) {
+		//Takes a HTML string then parses it and replaces appropriate
+		//links with the relevant expansion
+		//Returns a callback with the 'expanded' HTML string
+		Vue.prototype.$linkExpander = function (HTML, cb) {
+			let parsed = document.createElement('div');
+			parsed.innerHTML = HTML;
+
+			let expandPatterns = {
+				'wikipedia': {
+					hostnameRegExp: /[a-z]+\.wikipedia\.org/,
+					pathnameRegExp: /\/wiki\/.+/,
+					getURL (link) {
+						let page = link.pathname.split('/').slice(-1)[0];
+						let countryVersion = link.hostname.split('.')[0];
+						
+						return `https://${countryVersion}.wikipedia.org/api/rest_v1/page/summary/${page}?redirect=true`;
+					},
+					getContent (link, data) {
+						let content = data.extract.slice(0, 500).trim();
+
+						return {
+							title: data.titles.display,
+							URL: data.content_urls.desktop.page,
+							content: content.length > 500 ? content + '...' : content
+						}
+					}
+				},
+				'github': {
+					hostnameRegExp: /github\.com/,
+					pathnameRegExp: /\/.+\/.+/,
+					getURL (link) {
+						return 'https://api.github.com/repos' + link.pathname;
+					},
+					getContent (link, data) {
+						return {
+							title: data.full_name,
+							URL: data.html_url,
+							content: data.description
+						}
+					}
+				}
+			};
+
+			let links = Array
+				.from(parsed.querySelectorAll('p a[href]'))
+				.filter(a => {
+					return (
+						a.parentNode.parentNode === parsed &&
+						a.parentNode.childNodes.length === 1 &&
+						a.innerHTML === a.href
+					)
+				});
+
+			let expandableLinks = {};
+
+			links.forEach(link => {
+				for(let expandName in expandPatterns) {
+					let expand = expandPatterns[expandName];
+
+					if(
+						expand.hostnameRegExp.test(link.hostname) &&
+						expand.pathnameRegExp.test(link.pathname)
+					) {
+						if(!expandableLinks[expandName]) {
+							expandableLinks[expandName] = [];
+						}
+
+						expandableLinks[expandName].push(link);
+
+						break;
+					}
+				}
+			});
+
+			for(let expandName in expandableLinks) {
+				let expandPattern = expandPatterns[expandName];
+
+				expandableLinks[expandName].forEach(link => {
+					let URL = expandPattern.getURL(link);
+
+					Vue.axios
+						.get(URL)
+						.then(res => {
+							let content = expandPattern.getContent(link, res.data);
+							let h = document.createElement.bind(document);
+							
+							let div = h('div');
+							let h2 = h('h2');
+							let a = h('a');
+							let span = h('span');
+							let textNode = document.createTextNode(content.content);
+
+							a.textContent = content.title;
+							a.href = content.URL;
+							a.setAttribute('target', '_blank');
+							a.setAttribute('rel', 'noopener noreferrer');
+							span.textContent = 'from ' + link.hostname;
+
+							h2.appendChild(a);
+							h2.appendChild(span);
+							div.appendChild(h2)
+							div.appendChild(textNode)
+
+							div.classList.add('input_editor_preview__expandable');
+							link.parentNode.replaceChild(div, link);
+
+							completedAPICall();
+						})
+						.catch(completedAPICall);
+				});
+			}
+
+			let completed = 0;
+			let completedAPICall = () => {
+				completed++;
+
+				if(completed === links.length) {
+					cb(parsed.innerHTML);
+				}
+			}
+		}
+	}
+}

+ 1 - 121
frontend/src/components/InputEditorPreview.vue

@@ -42,128 +42,8 @@
 
 				let HTML = Marked(replacedMd);
 
-				this.expandURLs(HTML)
 				this.HTML = HTML;
-			},
-			expandURLs (HTML) {
-				let parsed = document.createElement('div');
-				parsed.innerHTML = HTML;
-
-				let expandPatterns = {
-					'wikipedia': {
-						hostnameRegExp: /[a-z]+\.wikipedia\.org/,
-						pathnameRegExp: /\/wiki\/.+/,
-						getURL (link) {
-							let page = link.pathname.split('/').slice(-1)[0];
-							let countryVersion = link.hostname.split('.')[0];
-							
-							return `https://${countryVersion}.wikipedia.org/api/rest_v1/page/summary/${page}?redirect=true`;
-						},
-						getContent (link, data) {
-							let content = data.extract.slice(0, 500).trim();
-
-							return {
-								title: data.titles.display,
-								URL: data.content_urls.desktop.page,
-								content: content.length > 500 ? content + '...' : content
-							}
-						}
-					},
-					'github': {
-						hostnameRegExp: /github\.com/,
-						pathnameRegExp: /\/.+\/.+/,
-						getURL (link) {
-							return 'https://api.github.com/repos' + link.pathname;
-						},
-						getContent (link, data) {
-							return {
-								title: data.full_name,
-								URL: data.html_url,
-								content: data.description
-							}
-						}
-					}
-				};
-
-				let links = Array
-					.from(parsed.querySelectorAll('p a[href]'))
-					.filter(a => {
-						return (
-							a.parentNode.parentNode === parsed &&
-							a.parentNode.childNodes.length === 1 &&
-							a.innerHTML === a.href
-						)
-					});
-
-				let expandableLinks = {};
-
-				links.forEach(link => {
-					for(let expandName in expandPatterns) {
-						let expand = expandPatterns[expandName];
-
-						if(
-							expand.hostnameRegExp.test(link.hostname) &&
-							expand.pathnameRegExp.test(link.pathname)
-						) {
-							if(!expandableLinks[expandName]) {
-								expandableLinks[expandName] = [];
-							}
-
-							expandableLinks[expandName].push(link);
-
-							break;
-						}
-					}
-				});
-
-				for(let expandName in expandableLinks) {
-					let expandPattern = expandPatterns[expandName];
-
-					expandableLinks[expandName].forEach(link => {
-						let URL = expandPattern.getURL(link);
-
-						this.axios
-							.get(URL)
-							.then(res => {
-								let content = expandPattern.getContent(link, res.data);
-								let h = document.createElement.bind(document);
-								
-								let div = h('div');
-								let h2 = h('h2');
-								let a = h('a');
-								let span = h('span');
-								let textNode = document.createTextNode(content.content);
-
-								a.textContent = content.title;
-								a.href = content.URL;
-								a.setAttribute('target', '_blank');
-								a.setAttribute('rel', 'noopener noreferrer');
-								span.textContent = 'from ' + link.hostname;
-
-								h2.appendChild(a);
-								h2.appendChild(span);
-								div.appendChild(h2)
-								div.appendChild(textNode)
-
-								div.classList.add('input_editor_preview__expandable');
-								link.parentNode.replaceChild(div, link);
-
-								completedAPICall();
-							})
-							.catch(e => {
-								completedAPICall();
-							})
-					});
-				}
-
-				let completed = 0;
-				let completedAPICall = () => {
-					completed++;
-
-					if(completed === links.length) {
-						this.HTML = parsed.innerHTML;
-					}
-				}
+				this.$linkExpander(HTML, v => this.HTML = v);
 			}
 		}
 	}

+ 3 - 0
frontend/src/main.js

@@ -25,6 +25,8 @@ import Vuex from 'vuex'
 import axios from 'axios'
 import VueAxios from 'vue-axios'
 
+import linkExpander from './assets/js/linkExpander'
+
 import App from './App'
 import store from './store/index'
 
@@ -54,6 +56,7 @@ import NotFound from './components/routes/NotFound'
 Vue.use(VueRouter)
 Vue.use(Vuex)
 Vue.use(VueAxios, axios)
+Vue.use(linkExpander)
 
 const router = new VueRouter({
 	routes: [