Skip to content

Commit

Permalink
Add component to encapsulate new-tab Markdown logic
Browse files Browse the repository at this point in the history
This allows us to do the renderer plugin typecast just once rather than
everywhere the Markdown component is used.
  • Loading branch information
rmunn committed Dec 3, 2024
1 parent 1bd7706 commit 5f37211
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 34 deletions.
10 changes: 10 additions & 0 deletions frontend/src/lib/components/Markdown/NewTabLinkMarkdown.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script lang="ts">
import Markdown, {type ComponentsMap} from 'svelte-exmarkdown';
import type {Component} from 'svelte';
import NewTabLinkRenderer from './NewTabLinkRenderer.svelte';
export let md: string;
const renderer: ComponentsMap = { a: NewTabLinkRenderer as unknown as Component };
</script>

<Markdown {md} plugins={[{ renderer: renderer }]} />
1 change: 1 addition & 0 deletions frontend/src/lib/components/Markdown/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as NewTabLinkRenderer } from './NewTabLinkRenderer.svelte';
export { default as NewTabLinkMarkdown } from './NewTabLinkMarkdown.svelte';
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import Checkbox from '../../forms/Checkbox.svelte';
import { slide } from 'svelte/transition';
import { Icon } from '$lib/icons';
import Markdown from 'svelte-exmarkdown';
import { NewTabLinkRenderer } from '$lib/components/Markdown';
import type {Component} from 'svelte';
import {NewTabLinkMarkdown} from '$lib/components/Markdown';
export let value: boolean;
</script>
Expand All @@ -22,6 +20,6 @@
{#if value}
<div class="alert alert-warning mt-4" transition:slide>
<Icon icon="i-mdi-alert-outline" />
<Markdown md={$t('project.confidential.suggestions')} plugins={[{ renderer: { a: NewTabLinkRenderer as Component<any> } }]} />
<NewTabLinkMarkdown md={$t('project.confidential.suggestions')} />
</div>
{/if}
10 changes: 3 additions & 7 deletions frontend/src/lib/components/Users/CreateUserModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import { helpLinks } from '$lib/components/help';
import { type LexAuthUser, type RegisterResponse } from '$lib/user';
import CreateUser from '$lib/components/Users/CreateUser.svelte';
import Markdown from 'svelte-exmarkdown';
import { NewTabLinkRenderer } from '$lib/components/Markdown';
import type {Component} from 'svelte';
import {NewTabLinkMarkdown} from '$lib/components/Markdown';
import Icon from '$lib/icons/Icon.svelte';
import { createEventDispatcher } from 'svelte';
Expand All @@ -30,13 +28,11 @@
<div>
<h3 class="text-lg">{$t('common.did_you_know')}</h3>
<div>
<Markdown
<NewTabLinkMarkdown
md={$t('admin_dashboard.create_user_modal.help_create_single_guest_user', { helpLink: helpLinks.addProjectMember })}
plugins={[{ renderer: { a: NewTabLinkRenderer as Component<any> } }]}
/>
<Markdown
<NewTabLinkMarkdown
md={$t('admin_dashboard.create_user_modal.help_create_bulk_guest_users', { helpLink: helpLinks.bulkAddCreate })}
plugins={[{ renderer: { a: NewTabLinkRenderer as Component<any> } }]}
/>
</div>
</div>
Expand Down
6 changes: 2 additions & 4 deletions frontend/src/lib/forms/Checkbox.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<script lang="ts">
import Markdown from 'svelte-exmarkdown';
import {NewTabLinkMarkdown} from '$lib/components/Markdown';
import FormFieldError from './FormFieldError.svelte';
import { randomFormId } from './utils';
import { NewTabLinkRenderer } from '$lib/components/Markdown';
import type {Component} from 'svelte';
export let label: string;
export let value: boolean;
Expand All @@ -24,7 +22,7 @@
{#if description}
<label for={id} class="label pb-0">
<span class="label-text-alt">
<Markdown md={description} plugins={[{ renderer: { a: NewTabLinkRenderer as Component<any> } }]} />
<NewTabLinkMarkdown md={description} />
</span>
</label>
{/if}
Expand Down
6 changes: 2 additions & 4 deletions frontend/src/lib/forms/FormError.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<script lang="ts">
import Markdown from 'svelte-exmarkdown';
import {NewTabLinkMarkdown} from '$lib/components/Markdown';
import type { ErrorMessage } from './types';
import { NewTabLinkRenderer } from '$lib/components/Markdown';
import type {Component} from 'svelte';
export let error: ErrorMessage = undefined;
export let markdown = false;
Expand All @@ -12,7 +10,7 @@
{#if error}
<span class="label text-lg text-error" class:justify-end={right}>
{#if markdown}
<Markdown md={error} plugins={[{ renderer: { a: NewTabLinkRenderer as Component<any> } }]} />
<NewTabLinkMarkdown md={error} />
{:else}
{error}
{/if}
Expand Down
6 changes: 2 additions & 4 deletions frontend/src/lib/forms/FormField.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
import { onMount } from 'svelte';
import FormFieldError from './FormFieldError.svelte';
import { randomFormId } from './utils';
import Markdown from 'svelte-exmarkdown';
import { NewTabLinkRenderer } from '$lib/components/Markdown';
import type {Component} from 'svelte';
import {NewTabLinkMarkdown} from '$lib/components/Markdown';
import type { HelpLink } from '$lib/components/help';
import SupHelp from '$lib/components/help/SupHelp.svelte';
Expand Down Expand Up @@ -41,7 +39,7 @@
{#if description}
<label for={id} class="label pb-0">
<span class="label-text-alt description">
<Markdown md={description} plugins={[{ renderer: { a: NewTabLinkRenderer as Component<any> } }]} />
<NewTabLinkMarkdown md={description} />
</span>
</label>
{/if}
Expand Down
6 changes: 2 additions & 4 deletions frontend/src/lib/forms/RadioButtonGroup.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
</script>
<script lang="ts">
import Markdown from 'svelte-exmarkdown';
import {NewTabLinkMarkdown} from '$lib/components/Markdown';
import FormFieldError from './FormFieldError.svelte';
import { randomFormId } from './utils';
import { NewTabLinkRenderer } from '$lib/components/Markdown';
import type {Component} from 'svelte';
export let buttons: SingleRadioButton[];
export let label: string; // This is for the group as a whole
Expand Down Expand Up @@ -45,7 +43,7 @@
{#if description}
<label for={id} class="label pb-0">
<span class="label-text-alt">
<Markdown md={description} plugins={[{ renderer: { a: NewTabLinkRenderer as Component<any> } }]} />
<NewTabLinkMarkdown md={description} />
</span>
</label>
{/if}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<script lang="ts">
import Modal from '$lib/components/modals/Modal.svelte';
import Markdown from 'svelte-exmarkdown';
import {NewTabLinkMarkdown} from '$lib/components/Markdown';
import type { Project } from './+page';
import t from '$lib/i18n';
import OpenInFlexButton from './OpenInFlexButton.svelte';
import SendReceiveUrlField from './SendReceiveUrlField.svelte';
import { NewTabLinkRenderer } from '$lib/components/Markdown';
import type {Component} from 'svelte';
export let project: Project;
let modal: Modal;
Expand All @@ -21,7 +20,7 @@
<h3>{$t('project_page.open_with_flex.button')}</h3>
<div class="alert alert-info mb-4 not-prose">
<span class="i-mdi-info-outline text-xl"></span>
<Markdown md={$t('project_page.open_with_flex.supported_version')} plugins={[{ renderer: { a: NewTabLinkRenderer as Component<any> } }]} />
<NewTabLinkMarkdown md={$t('project_page.open_with_flex.supported_version')} />
</div>
<div class="not-prose">
<Markdown md={$t('project_page.open_with_flex.instructions')} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
import { useNotifications } from '$lib/notify';
import { Duration, deriveAsync, deriveAsyncIfDefined } from '$lib/util/time';
import { getSearchParamValues } from '$lib/util/query-params';
import { onMount, type Component } from 'svelte';
import { onMount } from 'svelte';
import MemberBadge from '$lib/components/Badges/MemberBadge.svelte';
import { derived, writable, type Readable } from 'svelte/store';
import { concatAll } from '$lib/util/array';
import { browser } from '$app/environment';
import { ProjectConfidentialityCombobox } from '$lib/components/Projects';
import { _getProjectsByLangCodeAndOrg, _getProjectsByNameAndOrg } from './+page';
import Markdown from 'svelte-exmarkdown';
import { NewTabLinkRenderer } from '$lib/components/Markdown';
import {NewTabLinkMarkdown} from '$lib/components/Markdown';
import Button from '$lib/forms/Button.svelte';
import {projectUrl} from '$lib/util/project';
Expand Down Expand Up @@ -287,7 +286,7 @@
{/each}
<label for="group-extra-projects" class="label pb-0">
<span class="label-text-alt">
<Markdown md={$t('project.create.maybe_related_description')} plugins={[{ renderer: { a: NewTabLinkRenderer as Component<any> } }]} />
<NewTabLinkMarkdown md={$t('project.create.maybe_related_description')} />
</span>
</label>
</div>
Expand Down

0 comments on commit 5f37211

Please sign in to comment.