replace allowedChars with not-whitespace (#617)

This commit is contained in:
mrdrogdrog 2020-09-30 23:58:31 +02:00 committed by GitHub
parent 1ab9b58031
commit 733df9b94a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 14 additions and 29 deletions

View file

@ -1,14 +1,13 @@
import { Editor, Hint, Hints, Pos } from 'codemirror'
import { findWordAtCursor, Hinter, search } from './index'
const allowedChars = /[`\w-_+]/
const wordRegExp = /^```((\w|-|_|\+)*)$/
let allSupportedLanguages: string[] = []
const codeBlockHint = (editor: Editor): Promise< Hints| null > => {
return import(/* webpackChunkName: "highlight.js" */ 'highlight.js').then(hljs =>
new Promise((resolve) => {
const searchTerm = findWordAtCursor(editor, allowedChars)
const searchTerm = findWordAtCursor(editor)
const searchResult = wordRegExp.exec(searchTerm.text)
if (searchResult === null) {
resolve(null)
@ -36,7 +35,6 @@ const codeBlockHint = (editor: Editor): Promise< Hints| null > => {
}
export const CodeBlockHinter: Hinter = {
allowedChars,
wordRegExp,
hint: codeBlockHint
}

View file

@ -1,12 +1,11 @@
import { Editor, Hint, Hints, Pos } from 'codemirror'
import { findWordAtCursor, Hinter } from './index'
const allowedChars = /[<\w>]/
const wordRegExp = /^(<d(?:e|et|eta|etai|etail|etails)?)$/
const collapsableBlockHint = (editor: Editor): Promise< Hints| null > => {
return new Promise((resolve) => {
const searchTerm = findWordAtCursor(editor, allowedChars)
const searchTerm = findWordAtCursor(editor)
const searchResult = wordRegExp.exec(searchTerm.text)
if (searchResult === null) {
resolve(null)
@ -29,7 +28,6 @@ const collapsableBlockHint = (editor: Editor): Promise< Hints| null > => {
}
export const CollapsableBlockHinter: Hinter = {
allowedChars,
wordRegExp,
hint: collapsableBlockHint
}

View file

@ -1,13 +1,12 @@
import { Editor, Hint, Hints, Pos } from 'codemirror'
import { findWordAtCursor, Hinter } from './index'
const allowedChars = /[:\w-_+]/
const wordRegExp = /^:::((\w|-|_|\+)*)$/
const allSupportedConatiner = ['success', 'info', 'warning', 'danger']
const containerHint = (editor: Editor): Promise< Hints| null > => {
return new Promise((resolve) => {
const searchTerm = findWordAtCursor(editor, allowedChars)
const searchTerm = findWordAtCursor(editor)
const searchResult = wordRegExp.exec(searchTerm.text)
if (searchResult === null) {
resolve(null)
@ -31,7 +30,6 @@ const containerHint = (editor: Editor): Promise< Hints| null > => {
}
export const ContainerHinter: Hinter = {
allowedChars,
wordRegExp,
hint: containerHint
}

View file

@ -5,13 +5,12 @@ import { customEmojis } from '../tool-bar/emoji-picker/emoji-picker'
import { getEmojiIcon, getEmojiShortCode } from '../tool-bar/utils/emojiUtils'
import { findWordAtCursor, Hinter } from './index'
const allowedCharsInEmojiCodeRegex = /[:\w-_+]/
const emojiIndex = new NimbleEmojiIndex(data as unknown as Data)
const emojiWordRegex = /^:([\w-_+]*)$/
const generateEmojiHints = (editor: Editor): Promise< Hints| null > => {
return new Promise((resolve) => {
const searchTerm = findWordAtCursor(editor, allowedCharsInEmojiCodeRegex)
const searchTerm = findWordAtCursor(editor)
const searchResult = emojiWordRegex.exec(searchTerm.text)
if (searchResult === null) {
resolve(null)
@ -50,7 +49,6 @@ const generateEmojiHints = (editor: Editor): Promise< Hints| null > => {
}
export const EmojiHinter: Hinter = {
allowedChars: allowedCharsInEmojiCodeRegex,
wordRegExp: emojiWordRegex,
hint: generateEmojiHints
}

View file

@ -1,14 +1,13 @@
import { Editor, Hint, Hints, Pos } from 'codemirror'
import { findWordAtCursor, Hinter, search } from './index'
const allowedChars = /#/
const wordRegExp = /^(\s{0,3})(#{1,6})$/
const allSupportedHeaders = ['# h1', '## h2', '### h3', '#### h4', '##### h5', '###### h6', '###### tags: `example`']
const allSupportedHeadersTextToInsert = ['# ', '## ', '### ', '#### ', '##### ', '###### ', '###### tags: `example`']
const headerHint = (editor: Editor): Promise< Hints| null > => {
return new Promise((resolve) => {
const searchTerm = findWordAtCursor(editor, allowedChars)
const searchTerm = findWordAtCursor(editor)
const searchResult = wordRegExp.exec(searchTerm.text)
if (searchResult === null) {
resolve(null)
@ -25,8 +24,8 @@ const headerHint = (editor: Editor): Promise< Hints| null > => {
resolve(null)
} else {
resolve({
list: suggestions.map((suggestion, index): Hint => ({
text: allSupportedHeadersTextToInsert[index],
list: suggestions.map((suggestion): Hint => ({
text: allSupportedHeadersTextToInsert[allSupportedHeaders.indexOf(suggestion)],
displayText: suggestion
})),
from: Pos(cursor.line, searchTerm.start),
@ -37,7 +36,6 @@ const headerHint = (editor: Editor): Promise< Hints| null > => {
}
export const HeaderHinter: Hinter = {
allowedChars,
wordRegExp,
hint: headerHint
}

View file

@ -1,7 +1,6 @@
import { Editor, Hint, Hints, Pos } from 'codemirror'
import { findWordAtCursor, Hinter } from './index'
const allowedChars = /[![\]\w]/
const wordRegExp = /^(!(\[.*])?)$/
const allSupportedImages = [
'![image alt](https:// "title")',
@ -11,7 +10,7 @@ const allSupportedImages = [
const imageHint = (editor: Editor): Promise< Hints| null > => {
return new Promise((resolve) => {
const searchTerm = findWordAtCursor(editor, allowedChars)
const searchTerm = findWordAtCursor(editor)
const searchResult = wordRegExp.exec(searchTerm.text)
if (searchResult === null) {
resolve(null)
@ -34,7 +33,6 @@ const imageHint = (editor: Editor): Promise< Hints| null > => {
}
export const ImageHinter: Hinter = {
allowedChars,
wordRegExp,
hint: imageHint
}

View file

@ -15,12 +15,13 @@ interface findWordAtCursorResponse {
}
export interface Hinter {
allowedChars: RegExp,
wordRegExp: RegExp,
hint: (editor: Editor) => Promise< Hints| null >
}
export const findWordAtCursor = (editor: Editor, allowedChars: RegExp): findWordAtCursorResponse => {
const allowedChars = /[^\s]/
export const findWordAtCursor = (editor: Editor): findWordAtCursorResponse => {
const cursor = editor.getCursor()
const line = editor.getLine(cursor.line)
let start = cursor.ch

View file

@ -3,7 +3,6 @@ import moment from 'moment'
import { getUser } from '../../../../redux/user/methods'
import { findWordAtCursor, Hinter } from './index'
const allowedChars = /[[\]\w]/
const wordRegExp = /^(\[(.*])?)$/
const allSupportedLinks = [
'[link text](https:// "title")',
@ -22,7 +21,7 @@ const allSupportedLinks = [
const linkAndExtraTagHint = (editor: Editor): Promise< Hints| null > => {
return new Promise((resolve) => {
const searchTerm = findWordAtCursor(editor, allowedChars)
const searchTerm = findWordAtCursor(editor)
const searchResult = wordRegExp.exec(searchTerm.text)
if (searchResult === null) {
resolve(null)
@ -63,7 +62,6 @@ const linkAndExtraTagHint = (editor: Editor): Promise< Hints| null > => {
}
export const LinkAndExtraTagHinter: Hinter = {
allowedChars,
wordRegExp,
hint: linkAndExtraTagHint
}

View file

@ -1,12 +1,11 @@
import { Editor, Hint, Hints, Pos } from 'codemirror'
import { findWordAtCursor, Hinter } from './index'
const allowedChars = /[{%]/
const wordRegExp = /^({[%}]?)$/
const pdfHint = (editor: Editor): Promise< Hints| null > => {
return new Promise((resolve) => {
const searchTerm = findWordAtCursor(editor, allowedChars)
const searchTerm = findWordAtCursor(editor)
const searchResult = wordRegExp.exec(searchTerm.text)
if (searchResult === null) {
resolve(null)
@ -29,7 +28,6 @@ const pdfHint = (editor: Editor): Promise< Hints| null > => {
}
export const PDFHinter: Hinter = {
allowedChars,
wordRegExp,
hint: pdfHint
}

View file

@ -41,7 +41,7 @@ export interface EditorPaneProps {
const onChange = (editor: Editor) => {
for (const hinter of allHinters) {
const searchTerm = findWordAtCursor(editor, hinter.allowedChars)
const searchTerm = findWordAtCursor(editor)
if (hinter.wordRegExp.test(searchTerm.text)) {
editor.showHint({
hint: hinter.hint,