/** * Copyright (c) 2022 Amorphous * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ export const slugify = (...args: (string | number)[]): string => { const value = args.join(' ') return value .normalize('NFD') // split an accented letter in the base letter and the accent .replace(/[\u0300-\u036f]/g, '') // remove all previously split accents .toLowerCase() .trim() .replace(/[-_]/g, ' ') .replace(/[^a-z0-9 ]/g, '') // remove all chars not letters, numbers and spaces (to be replaced) .replace(/\s+/g, '-') // separator } type WikilinkResult = { target: string label: string } /** * * @param wikiLink the string of the link within the `[[]]` */ export const parseWikiLink = (wikiLink: string): WikilinkResult => { let label = wikiLink let target = wikiLink // display|target format const barIndex = wikiLink.indexOf('|') if (barIndex !== -1) { label = wikiLink.substring(0, barIndex) target = wikiLink.substring(barIndex + 1) } else { // display->target format const rightArrIndex = wikiLink.indexOf('->') if (rightArrIndex !== -1) { label = wikiLink.substring(0, rightArrIndex) target = wikiLink.substring(rightArrIndex + 2) } else { // target<-display format const leftArrIndex = wikiLink.indexOf('<-') if (leftArrIndex !== -1) { label = wikiLink.substring(leftArrIndex + 2) target = wikiLink.substring(0, leftArrIndex) } } } return { label, target, } }