Skip to content

Instantly share code, notes, and snippets.

@hackfrag
Created August 10, 2016 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hackfrag/e6e4d2d593db1a61bcb75d8c8d022e0e to your computer and use it in GitHub Desktop.
Save hackfrag/e6e4d2d593db1a61bcb75d8c8d022e0e to your computer and use it in GitHub Desktop.
import {Component, Input, DynamicComponentLoader, ElementRef, ViewContainerRef, Output, EventEmitter} from '@angular/core'
import {Config} from '../config';
@Component({
selector: 'wordpress-content',
template: '<div #container></div>',
})
export class WordpressContent {
@Input() content;
@Output() link = new EventEmitter();
constructor(private loader: DynamicComponentLoader, private elementRef: ElementRef,private viewContainerRef: ViewContainerRef, private config: Config) {
}
ngOnChanges(changes) {
var that = this;
if (changes.content.currentValue != null) {
let htmlTemplate = changes.content.currentValue;
htmlTemplate = htmlTemplate.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '');
htmlTemplate = htmlTemplate.replace(/<noscript\b[^<]*(?:(?!<\/noscript>)<[^<]*)*<\/noscript>/gi, '');
// Remove Links Arround Images
htmlTemplate = htmlTemplate.replace(/<a.*?(<img.*?>)<\/a>/gi, '$1');
// Detect Internal Links
htmlTemplate = htmlTemplate.replace(new RegExp('href=\"http:\/\/'+ this.config.baseUrl +'\/(.+?)\.html\"', 'g'), '(click)="onInteralLink(\'$1\')"');
// Fix broken Wordpress HTML (unclosed Tags etc)
var doc = document.createElement('div');
doc.innerHTML = htmlTemplate;
htmlTemplate = doc.innerHTML;
@Component({
selector: 'viewRenderer',
template: htmlTemplate,
})
class ViewRenderer {
model: any;
onInteralLink(page: string) {
that.link.emit(page);
}
}
this.loader.loadNextToLocation(ViewRenderer, this.viewContainerRef);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment