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

View file

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

View file

@ -1,13 +1,12 @@
import { Editor, Hint, Hints, Pos } from 'codemirror' import { Editor, Hint, Hints, Pos } from 'codemirror'
import { findWordAtCursor, Hinter } from './index' import { findWordAtCursor, Hinter } from './index'
const allowedChars = /[:\w-_+]/
const wordRegExp = /^:::((\w|-|_|\+)*)$/ const wordRegExp = /^:::((\w|-|_|\+)*)$/
const allSupportedConatiner = ['success', 'info', 'warning', 'danger'] const allSupportedConatiner = ['success', 'info', 'warning', 'danger']
const containerHint = (editor: Editor): Promise< Hints| null > => { const containerHint = (editor: Editor): Promise< Hints| null > => {
return new Promise((resolve) => { return new Promise((resolve) => {
const searchTerm = findWordAtCursor(editor, allowedChars) const searchTerm = findWordAtCursor(editor)
const searchResult = wordRegExp.exec(searchTerm.text) const searchResult = wordRegExp.exec(searchTerm.text)
if (searchResult === null) { if (searchResult === null) {
resolve(null) resolve(null)
@ -31,7 +30,6 @@ const containerHint = (editor: Editor): Promise< Hints| null > => {
} }
export const ContainerHinter: Hinter = { export const ContainerHinter: Hinter = {
allowedChars,
wordRegExp, wordRegExp,
hint: containerHint 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 { getEmojiIcon, getEmojiShortCode } from '../tool-bar/utils/emojiUtils'
import { findWordAtCursor, Hinter } from './index' import { findWordAtCursor, Hinter } from './index'
const allowedCharsInEmojiCodeRegex = /[:\w-_+]/
const emojiIndex = new NimbleEmojiIndex(data as unknown as Data) const emojiIndex = new NimbleEmojiIndex(data as unknown as Data)
const emojiWordRegex = /^:([\w-_+]*)$/ const emojiWordRegex = /^:([\w-_+]*)$/
const generateEmojiHints = (editor: Editor): Promise< Hints| null > => { const generateEmojiHints = (editor: Editor): Promise< Hints| null > => {
return new Promise((resolve) => { return new Promise((resolve) => {
const searchTerm = findWordAtCursor(editor, allowedCharsInEmojiCodeRegex) const searchTerm = findWordAtCursor(editor)
const searchResult = emojiWordRegex.exec(searchTerm.text) const searchResult = emojiWordRegex.exec(searchTerm.text)
if (searchResult === null) { if (searchResult === null) {
resolve(null) resolve(null)
@ -50,7 +49,6 @@ const generateEmojiHints = (editor: Editor): Promise< Hints| null > => {
} }
export const EmojiHinter: Hinter = { export const EmojiHinter: Hinter = {
allowedChars: allowedCharsInEmojiCodeRegex,
wordRegExp: emojiWordRegex, wordRegExp: emojiWordRegex,
hint: generateEmojiHints hint: generateEmojiHints
} }

View file

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

View file

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

View file

@ -15,12 +15,13 @@ interface findWordAtCursorResponse {
} }
export interface Hinter { export interface Hinter {
allowedChars: RegExp,
wordRegExp: RegExp, wordRegExp: RegExp,
hint: (editor: Editor) => Promise< Hints| null > 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 cursor = editor.getCursor()
const line = editor.getLine(cursor.line) const line = editor.getLine(cursor.line)
let start = cursor.ch let start = cursor.ch

View file

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

View file

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

View file

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