Add markdown support to plugins (#2654)
* Add markdown renderer to plugins * Chore: add doc for markdown plugins * Fix typing markdown plugin helpers * Add lines between components in template Co-authored-by: kimsible <kimsible@users.noreply.github.com>
This commit is contained in:
parent
2fd59d7d89
commit
8c7725dc3c
|
@ -8,9 +8,27 @@
|
||||||
<form *ngIf="hasRegisteredSettings()" role="form" (ngSubmit)="formValidated()" [formGroup]="form">
|
<form *ngIf="hasRegisteredSettings()" role="form" (ngSubmit)="formValidated()" [formGroup]="form">
|
||||||
<div class="form-group" *ngFor="let setting of registeredSettings">
|
<div class="form-group" *ngFor="let setting of registeredSettings">
|
||||||
<label *ngIf="setting.type !== 'input-checkbox'" [attr.for]="setting.name" [innerHTML]="setting.label"></label>
|
<label *ngIf="setting.type !== 'input-checkbox'" [attr.for]="setting.name" [innerHTML]="setting.label"></label>
|
||||||
|
|
||||||
<input *ngIf="setting.type === 'input'" type="text" [id]="setting.name" [formControlName]="setting.name" />
|
<input *ngIf="setting.type === 'input'" type="text" [id]="setting.name" [formControlName]="setting.name" />
|
||||||
|
|
||||||
<textarea *ngIf="setting.type === 'input-textarea'" type="text" [id]="setting.name" [formControlName]="setting.name"></textarea>
|
<textarea *ngIf="setting.type === 'input-textarea'" type="text" [id]="setting.name" [formControlName]="setting.name"></textarea>
|
||||||
|
|
||||||
|
<my-help *ngIf="setting.type === 'markdown-text'" helpType="markdownText"></my-help>
|
||||||
|
|
||||||
|
<my-help *ngIf="setting.type === 'markdown-enhanced'" helpType="markdownEnhanced"></my-help>
|
||||||
|
|
||||||
|
<my-markdown-textarea
|
||||||
|
*ngIf="setting.type === 'markdown-text'"
|
||||||
|
markdownType="text" [id]="setting.name" [formControlName]="setting.name" textareaWidth="500px" [previewColumn]="false"
|
||||||
|
[classes]="{ 'input-error': formErrors['settings.name'] }"
|
||||||
|
></my-markdown-textarea>
|
||||||
|
|
||||||
|
<my-markdown-textarea
|
||||||
|
*ngIf="setting.type === 'markdown-enhanced'"
|
||||||
|
markdownType="enhanced" [id]="setting.name" [formControlName]="setting.name" textareaWidth="500px" [previewColumn]="false"
|
||||||
|
[classes]="{ 'input-error': formErrors['settings.name'] }"
|
||||||
|
></my-markdown-textarea>
|
||||||
|
|
||||||
<my-peertube-checkbox
|
<my-peertube-checkbox
|
||||||
*ngIf="setting.type === 'input-checkbox'"
|
*ngIf="setting.type === 'input-checkbox'"
|
||||||
[id]="setting.name"
|
[id]="setting.name"
|
||||||
|
|
|
@ -15,6 +15,7 @@ import { HttpClient } from '@angular/common/http'
|
||||||
import { AuthService } from '@app/core/auth'
|
import { AuthService } from '@app/core/auth'
|
||||||
import { Notifier } from '@app/core/notification'
|
import { Notifier } from '@app/core/notification'
|
||||||
import { RestExtractor } from '@app/shared/rest'
|
import { RestExtractor } from '@app/shared/rest'
|
||||||
|
import { MarkdownService } from '@app/shared/renderer'
|
||||||
import { PluginType } from '@shared/models/plugins/plugin.type'
|
import { PluginType } from '@shared/models/plugins/plugin.type'
|
||||||
import { PublicServerSetting } from '@shared/models/plugins/public-server.setting'
|
import { PublicServerSetting } from '@shared/models/plugins/public-server.setting'
|
||||||
import { getDevLocale, isOnDevLocale } from '@app/shared/i18n/i18n-utils'
|
import { getDevLocale, isOnDevLocale } from '@app/shared/i18n/i18n-utils'
|
||||||
|
@ -65,6 +66,7 @@ export class PluginService implements ClientHook {
|
||||||
private router: Router,
|
private router: Router,
|
||||||
private authService: AuthService,
|
private authService: AuthService,
|
||||||
private notifier: Notifier,
|
private notifier: Notifier,
|
||||||
|
private markdownRenderer: MarkdownService,
|
||||||
private server: ServerService,
|
private server: ServerService,
|
||||||
private zone: NgZone,
|
private zone: NgZone,
|
||||||
private authHttp: HttpClient,
|
private authHttp: HttpClient,
|
||||||
|
@ -297,6 +299,16 @@ export class PluginService implements ClientHook {
|
||||||
this.customModal.show(input)
|
this.customModal.show(input)
|
||||||
},
|
},
|
||||||
|
|
||||||
|
markdownRenderer: {
|
||||||
|
textMarkdownToHTML: (textMarkdown: string) => {
|
||||||
|
return this.markdownRenderer.textMarkdownToHTML(textMarkdown)
|
||||||
|
},
|
||||||
|
|
||||||
|
enhancedMarkdownToHTML: (enhancedMarkdown: string) => {
|
||||||
|
return this.markdownRenderer.enhancedMarkdownToHTML(enhancedMarkdown)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
translate: (value: string) => {
|
translate: (value: string) => {
|
||||||
return this.translationsObservable
|
return this.translationsObservable
|
||||||
.pipe(map(allTranslations => allTranslations[npmName]))
|
.pipe(map(allTranslations => allTranslations[npmName]))
|
||||||
|
|
|
@ -27,5 +27,10 @@ export type RegisterClientHelpers = {
|
||||||
confirm?: { value: string, action?: () => void }
|
confirm?: { value: string, action?: () => void }
|
||||||
}) => void
|
}) => void
|
||||||
|
|
||||||
|
markdownRenderer: {
|
||||||
|
textMarkdownToHTML: (textMarkdown: string) => Promise<string>
|
||||||
|
enhancedMarkdownToHTML: (enhancedMarkdown: string) => Promise<string>
|
||||||
|
}
|
||||||
|
|
||||||
translate: (toTranslate: string) => Promise<string>
|
translate: (toTranslate: string) => Promise<string>
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
export interface RegisterServerSettingOptions {
|
export interface RegisterServerSettingOptions {
|
||||||
name: string
|
name: string
|
||||||
label: string
|
label: string
|
||||||
type: 'input' | 'input-checkbox' | 'input-textarea'
|
type: 'input' | 'input-checkbox' | 'input-textarea' | 'markdown-text' | 'markdown-enhanced'
|
||||||
|
|
||||||
// If the setting is not private, anyone can view its value (client code included)
|
// If the setting is not private, anyone can view its value (client code included)
|
||||||
// If the setting is private, only server-side hooks can access it
|
// If the setting is private, only server-side hooks can access it
|
||||||
|
|
|
@ -149,6 +149,7 @@ registerSetting({
|
||||||
name: 'admin-name',
|
name: 'admin-name',
|
||||||
label: 'Admin name',
|
label: 'Admin name',
|
||||||
type: 'input',
|
type: 'input',
|
||||||
|
// type: input | input-checkbox | input-textarea | markdown-text | markdown-enhanced
|
||||||
default: 'my super name'
|
default: 'my super name'
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -216,6 +217,20 @@ notifier.success('Success message content.')
|
||||||
notifier.error('Error message content.')
|
notifier.error('Error message content.')
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Markdown Renderer
|
||||||
|
|
||||||
|
To render a formatted markdown text to HTML:
|
||||||
|
|
||||||
|
```js
|
||||||
|
const { markdownRenderer } = peertubeHelpers
|
||||||
|
|
||||||
|
await markdownRenderer.textMarkdownToHTML('**My Bold Text**')
|
||||||
|
// return <strong>My Bold Text</strong>
|
||||||
|
|
||||||
|
await markdownRenderer.enhancedMarkdownToHTML('![alt-img](http://.../my-image.jpg)')
|
||||||
|
// return <img alt=alt-img src=http://.../my-image.jpg />
|
||||||
|
```
|
||||||
|
|
||||||
#### Custom Modal
|
#### Custom Modal
|
||||||
|
|
||||||
To show a custom modal:
|
To show a custom modal:
|
||||||
|
|
Loading…
Reference in New Issue