mirror of
https://github.com/LemmyNet/lemmy-ui.git
synced 2024-11-01 10:09:56 +00:00
Move regex pattern to config
This commit is contained in:
parent
c5779cd9b1
commit
fc0c063426
2 changed files with 14 additions and 13 deletions
|
@ -25,4 +25,14 @@ export const fetchLimit = 40;
|
||||||
export const relTags = "noopener nofollow";
|
export const relTags = "noopener nofollow";
|
||||||
export const emDash = "\u2014";
|
export const emDash = "\u2014";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Accepted formats:
|
||||||
|
* !community@server.com
|
||||||
|
* /c/community@server.com
|
||||||
|
* /m/community@server.com
|
||||||
|
* /u/username@server.com
|
||||||
|
*/
|
||||||
|
export const instanceLinkRegex =
|
||||||
|
/(\/[c|m|u]\/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}|![a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})/g;
|
||||||
|
|
||||||
export const testHost = "0.0.0.0:8536";
|
export const testHost = "0.0.0.0:8536";
|
||||||
|
|
|
@ -14,6 +14,7 @@ import markdown_it_sub from "markdown-it-sub";
|
||||||
import markdown_it_sup from "markdown-it-sup";
|
import markdown_it_sup from "markdown-it-sup";
|
||||||
import Renderer from "markdown-it/lib/renderer";
|
import Renderer from "markdown-it/lib/renderer";
|
||||||
import Token from "markdown-it/lib/token";
|
import Token from "markdown-it/lib/token";
|
||||||
|
import { instanceLinkRegex } from "./config";
|
||||||
|
|
||||||
export let Tribute: any;
|
export let Tribute: any;
|
||||||
|
|
||||||
|
@ -74,16 +75,6 @@ const html5EmbedConfig = {
|
||||||
|
|
||||||
function localCommunityLinkParser(md) {
|
function localCommunityLinkParser(md) {
|
||||||
md.core.ruler.push("replace-text", state => {
|
md.core.ruler.push("replace-text", state => {
|
||||||
/**
|
|
||||||
* Accepted formats:
|
|
||||||
* !community@server.com
|
|
||||||
* /c/community@server.com
|
|
||||||
* /m/community@server.com
|
|
||||||
* /u/username@server.com
|
|
||||||
*/
|
|
||||||
const pattern =
|
|
||||||
/(\/[c|m|u]\/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}|![a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})/g;
|
|
||||||
|
|
||||||
for (let i = 0; i < state.tokens.length; i++) {
|
for (let i = 0; i < state.tokens.length; i++) {
|
||||||
if (state.tokens[i].type !== "inline") {
|
if (state.tokens[i].type !== "inline") {
|
||||||
continue;
|
continue;
|
||||||
|
@ -92,14 +83,14 @@ function localCommunityLinkParser(md) {
|
||||||
for (let j = inlineTokens.length - 1; j >= 0; j--) {
|
for (let j = inlineTokens.length - 1; j >= 0; j--) {
|
||||||
if (
|
if (
|
||||||
inlineTokens[j].type === "text" &&
|
inlineTokens[j].type === "text" &&
|
||||||
pattern.test(inlineTokens[j].content)
|
instanceLinkRegex.test(inlineTokens[j].content)
|
||||||
) {
|
) {
|
||||||
const textParts = inlineTokens[j].content.split(pattern);
|
const textParts = inlineTokens[j].content.split(instanceLinkRegex);
|
||||||
const newTokens: Token[] = [];
|
const newTokens: Token[] = [];
|
||||||
|
|
||||||
for (const part of textParts) {
|
for (const part of textParts) {
|
||||||
let linkClass = "community-link";
|
let linkClass = "community-link";
|
||||||
if (pattern.test(part)) {
|
if (instanceLinkRegex.test(part)) {
|
||||||
// Rewrite !community@server.com and KBin /m/community@server.com to local urls
|
// Rewrite !community@server.com and KBin /m/community@server.com to local urls
|
||||||
let href;
|
let href;
|
||||||
if (part.startsWith("!")) {
|
if (part.startsWith("!")) {
|
||||||
|
|
Loading…
Reference in a new issue