Add markdown support to plugins (#2654)
authorKim <1877318+kimsible@users.noreply.github.com>
Mon, 20 Apr 2020 12:51:24 +0000 (14:51 +0200)
committerGitHub <noreply@github.com>
Mon, 20 Apr 2020 12:51:24 +0000 (14:51 +0200)
* 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>
client/src/app/+admin/plugins/plugin-show-installed/plugin-show-installed.component.html
client/src/app/core/plugins/plugin.service.ts
client/src/types/register-client-option.model.ts
shared/models/plugins/register-server-setting.model.ts
support/doc/plugins/guide.md

index 95dd74d31cd1a21a84d949de7810f187fe1a31cc..bf135ecbd50b02365eedc0dcecc626e8a53f4dae 100644 (file)
@@ -8,9 +8,27 @@
   <form *ngIf="hasRegisteredSettings()" role="form" (ngSubmit)="formValidated()" [formGroup]="form">
     <div class="form-group" *ngFor="let setting of registeredSettings">
       <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" />
+
       <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
         *ngIf="setting.type === 'input-checkbox'"
         [id]="setting.name"
index 039fd6ff1ee8108f6a955a223afad790b7b0c8c6..3ed23160dfb5d9366108ed263f5fe85a22e25b3c 100644 (file)
@@ -15,6 +15,7 @@ import { HttpClient } from '@angular/common/http'
 import { AuthService } from '@app/core/auth'
 import { Notifier } from '@app/core/notification'
 import { RestExtractor } from '@app/shared/rest'
+import { MarkdownService } from '@app/shared/renderer'
 import { PluginType } from '@shared/models/plugins/plugin.type'
 import { PublicServerSetting } from '@shared/models/plugins/public-server.setting'
 import { getDevLocale, isOnDevLocale } from '@app/shared/i18n/i18n-utils'
@@ -65,6 +66,7 @@ export class PluginService implements ClientHook {
     private router: Router,
     private authService: AuthService,
     private notifier: Notifier,
+    private markdownRenderer: MarkdownService,
     private server: ServerService,
     private zone: NgZone,
     private authHttp: HttpClient,
@@ -297,6 +299,16 @@ export class PluginService implements ClientHook {
         this.customModal.show(input)
       },
 
+      markdownRenderer: {
+        textMarkdownToHTML: (textMarkdown: string) => {
+          return this.markdownRenderer.textMarkdownToHTML(textMarkdown)
+        },
+
+        enhancedMarkdownToHTML: (enhancedMarkdown: string) => {
+          return this.markdownRenderer.enhancedMarkdownToHTML(enhancedMarkdown)
+        }
+      },
+
       translate: (value: string) => {
         return this.translationsObservable
             .pipe(map(allTranslations => allTranslations[npmName]))
index 1c235107a37cc622b9fe35a375c51f0b5086ae84..dff00e9dd93d924c4e6d98f32667dbc4bca5be79 100644 (file)
@@ -27,5 +27,10 @@ export type RegisterClientHelpers = {
     confirm?: { value: string, action?: () => void }
   }) => void
 
+  markdownRenderer: {
+    textMarkdownToHTML: (textMarkdown: string) => Promise<string>
+    enhancedMarkdownToHTML: (enhancedMarkdown: string) => Promise<string>
+  }
+
   translate: (toTranslate: string) => Promise<string>
 }
index 45d79228d64196c1be76ee24607a1841a00c46a0..ec175e9ef320d0dac012b422324eb9eed050e030 100644 (file)
@@ -1,7 +1,7 @@
 export interface RegisterServerSettingOptions {
   name: 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 private, only server-side hooks can access it
index e6870ce173c864fed8ca0f9a6ab4e08cb0cac687..1bee1f611418a5e974222937443aceddb8660b2d 100644 (file)
@@ -149,6 +149,7 @@ registerSetting({
   name: 'admin-name',
   label: 'Admin name',
   type: 'input',
+  // type: input | input-checkbox | input-textarea | markdown-text | markdown-enhanced
   default: 'my super name'
 })
 
@@ -216,6 +217,20 @@ notifier.success('Success 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
 
 To show a custom modal: