Changeset View
Changeset View
Standalone View
Standalone View
src/app/_pipes/marked.pipe.ts
import { Pipe, PipeTransform } from '@angular/core'; | import { Pipe, PipeTransform } from '@angular/core'; | ||||
import { Comment } from '../_models/comment'; | import { Comment } from '../_models/comment'; | ||||
import { Post } from '../_models/post'; | import { Post } from '../_models/post'; | ||||
import * as C from '../_models/constants' | import * as C from '../_models/constants'; | ||||
import * as marked from 'markdown-it'; | import * as marked from 'markdown-it'; | ||||
import markdownItRegex from 'markdown-it-regex'; | import markdownItRegex from 'markdown-it-regex'; | ||||
const md = new marked('default', { | const md = new marked('default', { | ||||
linkify: true, | linkify: true, | ||||
html: false, | html: false, | ||||
xhtmlOut: true | xhtmlOut: true | ||||
}); | }); | ||||
Show All 14 Lines | getImageMatchesByGroup(index: number, str: string, reg: RegExp): string[] { | ||||
return matches; | return matches; | ||||
} | } | ||||
transform(value: string, post: Post, comment: Comment): string { | transform(value: string, post: Post, comment: Comment): string { | ||||
md.use(markdownItRegex, { | md.use(markdownItRegex, { | ||||
name: 'customImage', | name: 'customImage', | ||||
regex: /(\[img: \d\])/gm, | regex: /(\[img: \d\])/gm, | ||||
replace: (match: string) => { | replace: (match: string) => { | ||||
const index = Number.parseInt(this.getImageMatchesByGroup(2, match, /(\[img: (\d)\])/gm)[0]); | const index = parseInt(this.getImageMatchesByGroup(2, match, /(\[img: (\d)\])/gm)[0], 10); | ||||
if (post) { | if (post) { | ||||
if (!post.additional_images[index]) { | if (!post.additional_images[index]) { | ||||
return ''; | return ''; | ||||
} | } | ||||
} | } | ||||
if (comment) { | if (comment) { | ||||
Show All 15 Lines |