Open links always in new tabs (#912)

Signed-off-by: Tilman Vatteroth <tilman.vatteroth@tu-dortmund.de>
This commit is contained in:
Tilman Vatteroth 2021-01-03 00:06:13 +01:00 committed by GitHub
parent 184a972c26
commit 528e67e11e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 4 deletions

View file

@ -9,6 +9,7 @@ import { AbcReplacer } from '../replace-components/abc/abc-replacer'
import { AsciinemaReplacer } from '../replace-components/asciinema/asciinema-replacer'
import { ComponentReplacer } from '../replace-components/ComponentReplacer'
import { CsvReplacer } from '../replace-components/csv/csv-replacer'
import { LinkInNewTabReplacer } from '../replace-components/external-links-in-new-tabs/external-links-in-new-tabs'
import { FlowchartReplacer } from '../replace-components/flow/flowchart-replacer'
import { GistReplacer } from '../replace-components/gist/gist-replacer'
import { GraphvizReplacer } from '../replace-components/graphviz/graphviz-replacer'
@ -29,6 +30,7 @@ import { YoutubeReplacer } from '../replace-components/youtube/youtube-replacer'
export const useReplacerInstanceListCreator = (onTaskCheckedChange?: (lineInMarkdown: number, checked: boolean) => void): () => ComponentReplacer[] => {
return useMemo(() => () => [
new LinkInNewTabReplacer(),
new LinemarkerReplacer(),
new PossibleWiderReplacer(),
new GistReplacer(),

View file

@ -8,11 +8,13 @@ import anchor from '@mrdrogdrog/markdown-it-anchor'
import MarkdownIt from 'markdown-it'
export const headlineAnchors: MarkdownIt.PluginSimple = (markdownIt) => {
// noinspection CheckTagEmptyBody
anchor(markdownIt, {
const options: anchor.AnchorOptions = {
permalink: true,
permalinkBefore: true,
permalinkClass: 'heading-anchor text-dark',
permalinkSymbol: '<i class="fa fa-link"></i>'
})
permalinkSymbol: '<i class="fa fa-link"></i>',
permalinkHref: (slug: string): string => slug
}
anchor(markdownIt, options)
}

View file

@ -0,0 +1,24 @@
/*
* SPDX-FileCopyrightText: 2020 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { DomElement } from 'domhandler'
import { ReactElement } from 'react'
import { ComponentReplacer, SubNodeTransform } from '../ComponentReplacer'
export class LinkInNewTabReplacer extends ComponentReplacer {
public getReplacement (node: DomElement, subNodeTransform: SubNodeTransform): (ReactElement | null | undefined) {
const isJumpMark = node.attribs?.href?.substr(0, 1) === '#'
if (node.name !== 'a' || isJumpMark) {
return undefined
}
return <a {...node.attribs} rel='noopener noreferrer' target='_blank'>
{
node.children?.map((child, index) => subNodeTransform(child, index))
}
</a>
}
}