add experiment demos

This commit is contained in:
Philipp Kühn
2021-08-25 17:53:02 +02:00
parent 2498c24186
commit 6ab708b1a2
43 changed files with 3090 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
import LinterPlugin from '../LinterPlugin'
export class BadWords extends LinterPlugin {
public regex = /\b(obviously|clearly|evidently|simply)\b/ig
scan() {
this.doc.descendants((node: any, position: number) => {
if (!node.isText) {
return
}
const matches = this.regex.exec(node.text)
if (matches) {
this.record(
`Try not to say '${matches[0]}'`,
position + matches.index, position + matches.index + matches[0].length,
)
}
})
return this
}
}

View File

@@ -0,0 +1,30 @@
import { EditorView } from 'prosemirror-view'
import LinterPlugin, { Result as Issue } from '../LinterPlugin'
export class HeadingLevel extends LinterPlugin {
fixHeader(level: number) {
return function ({ state, dispatch }: EditorView, issue: Issue) {
dispatch(state.tr.setNodeMarkup(issue.from - 1, undefined, { level }))
}
}
scan() {
let lastHeadLevel: number | null = null
this.doc.descendants((node, position) => {
if (node.type.name === 'heading') {
// Check whether heading levels fit under the current level
const { level } = node.attrs
if (lastHeadLevel != null && level > lastHeadLevel + 1) {
this.record(`Heading too small (${level} under ${lastHeadLevel})`,
position + 1, position + 1 + node.content.size,
this.fixHeader(lastHeadLevel + 1))
}
lastHeadLevel = level
}
})
return this
}
}

View File

@@ -0,0 +1,37 @@
import { EditorView } from 'prosemirror-view'
import LinterPlugin, { Result as Issue } from '../LinterPlugin'
export class Punctuation extends LinterPlugin {
public regex = / ([,.!?:]) ?/g
fix(replacement: any) {
return function ({ state, dispatch }: EditorView, issue: Issue) {
dispatch(
state.tr.replaceWith(
issue.from, issue.to,
state.schema.text(replacement),
),
)
}
}
scan() {
this.doc.descendants((node, position) => {
if (!node.isText) return
if (!node.text) return
const matches = this.regex.exec(node.text)
if (matches) {
this.record(
'Suspicious spacing around punctuation',
position + matches.index, position + matches.index + matches[0].length,
this.fix(`${matches[1]} `),
)
}
})
return this
}
}