aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared
diff options
context:
space:
mode:
authorRigel Kent <sendmemail@rigelk.eu>2019-12-16 16:21:42 +0100
committerChocobozzz <chocobozzz@cpy.re>2019-12-17 09:45:02 +0100
commitb29bf61dbd518e5cef0b2f564ddc8f8a0657d089 (patch)
tree86d41fb765ea529095d757e292213156cef7d899 /client/src/app/shared
parentd68ebf0b4a40f88e53a78de6b3109a41466fa7c6 (diff)
downloadPeerTube-b29bf61dbd518e5cef0b2f564ddc8f8a0657d089.tar.gz
PeerTube-b29bf61dbd518e5cef0b2f564ddc8f8a0657d089.tar.zst
PeerTube-b29bf61dbd518e5cef0b2f564ddc8f8a0657d089.zip
Provide native links for description timestamps, and re-clickability for these
Diffstat (limited to 'client/src/app/shared')
-rw-r--r--client/src/app/shared/angular/timestamp-route-transformer.directive.ts45
-rw-r--r--client/src/app/shared/renderer/markdown.service.ts18
2 files changed, 54 insertions, 9 deletions
diff --git a/client/src/app/shared/angular/timestamp-route-transformer.directive.ts b/client/src/app/shared/angular/timestamp-route-transformer.directive.ts
new file mode 100644
index 000000000..d71077d10
--- /dev/null
+++ b/client/src/app/shared/angular/timestamp-route-transformer.directive.ts
@@ -0,0 +1,45 @@
1import { Directive, ElementRef, HostListener, Output, EventEmitter } from '@angular/core'
2import { Router } from '@angular/router'
3
4type ElementEvent = Omit<Event, 'target'> & {
5 target: HTMLInputElement
6}
7
8@Directive({
9 selector: '[timestampRouteTransformer]'
10})
11export class TimestampRouteTransformerDirective {
12 @Output() timestampClicked = new EventEmitter<number>()
13
14 constructor (private el: ElementRef, private router: Router) { }
15
16 @HostListener('click', ['$event'])
17 public onClick ($event: ElementEvent) {
18 if ($event.target.hasAttribute('href')) {
19 const ngxLink = document.createElement('a')
20 ngxLink.href = $event.target.getAttribute('href')
21
22 // we only care about reflective links
23 if (ngxLink.host !== window.location.host) return
24
25 const ngxLinkParams = new URLSearchParams(ngxLink.search)
26 if (ngxLinkParams.has('start')) {
27 const separators = ['h', 'm', 's']
28 const start = ngxLinkParams
29 .get('start')
30 .match(new RegExp('(\\d{1,9}[' + separators.join('') + '])','g')) // match digits before any given separator
31 .map(t => {
32 if (t.includes('h')) return parseInt(t, 10) * 3600
33 if (t.includes('m')) return parseInt(t, 10) * 60
34 return parseInt(t, 10)
35 })
36 .reduce((acc, t) => acc + t)
37 this.timestampClicked.emit(start)
38 }
39
40 $event.preventDefault()
41 }
42
43 return
44 }
45}
diff --git a/client/src/app/shared/renderer/markdown.service.ts b/client/src/app/shared/renderer/markdown.service.ts
index f6b71b88a..0d3fde537 100644
--- a/client/src/app/shared/renderer/markdown.service.ts
+++ b/client/src/app/shared/renderer/markdown.service.ts
@@ -75,6 +75,14 @@ export class MarkdownService {
75 return this.render('completeMarkdownIt', markdown) 75 return this.render('completeMarkdownIt', markdown)
76 } 76 }
77 77
78 async processVideoTimestamps (html: string) {
79 return html.replace(/((\d{1,2}):)?(\d{1,2}):(\d{1,2})/g, function (str, _, h, m, s) {
80 const t = (3600 * +(h || 0)) + (60 * +(m || 0)) + (+(s || 0))
81 const url = buildVideoLink({ startTime: t })
82 return `<a class="video-timestamp" href="${url}">${str}</a>`
83 })
84 }
85
78 private async render (name: keyof MarkdownParsers, markdown: string) { 86 private async render (name: keyof MarkdownParsers, markdown: string) {
79 if (!markdown) return '' 87 if (!markdown) return ''
80 88
@@ -91,14 +99,6 @@ export class MarkdownService {
91 return html 99 return html
92 } 100 }
93 101
94 async processVideoTimestamps (html: string) {
95 return html.replace(/((\d{1,2}):)?(\d{1,2}):(\d{1,2})/g, function (str, _, h, m, s) {
96 const t = (3600 * +(h || 0)) + (60 * +(m || 0)) + (+(s || 0))
97 const url = buildVideoLink({ startTime: t })
98 return `<a href="${url}">${str}</a>`
99 })
100 }
101
102 private async createMarkdownIt (config: MarkdownConfig) { 102 private async createMarkdownIt (config: MarkdownConfig) {
103 // FIXME: import('...') returns a struct module, containing a "default" field corresponding to our sanitizeHtml function 103 // FIXME: import('...') returns a struct module, containing a "default" field corresponding to our sanitizeHtml function
104 const MarkdownItClass: typeof import ('markdown-it') = (await import('markdown-it') as any).default 104 const MarkdownItClass: typeof import ('markdown-it') = (await import('markdown-it') as any).default
@@ -139,7 +139,7 @@ export class MarkdownService {
139 private avoidTruncatedTags (html: string) { 139 private avoidTruncatedTags (html: string) {
140 return html.replace(/\*\*?([^*]+)$/, '$1') 140 return html.replace(/\*\*?([^*]+)$/, '$1')
141 .replace(/<a[^>]+>([^<]+)<\/a>\s*...((<\/p>)|(<\/li>)|(<\/strong>))?$/mi, '$1...') 141 .replace(/<a[^>]+>([^<]+)<\/a>\s*...((<\/p>)|(<\/li>)|(<\/strong>))?$/mi, '$1...')
142 .replace(/\[[^\]]+\]?\(?([^\)]+)$/, '$1') 142 .replace(/\[[^\]]+\]\(([^\)]+)$/m, '$1')
143 .replace(/\s?\[[^\]]+\]?[.]{3}<\/p>$/m, '...</p>') 143 .replace(/\s?\[[^\]]+\]?[.]{3}<\/p>$/m, '...</p>')
144 } 144 }
145} 145}