-
Notifications
You must be signed in to change notification settings - Fork 66.9k
Expand file tree
/
Copy pathlink-punctuation.ts
More file actions
41 lines (38 loc) · 1.42 KB
/
link-punctuation.ts
File metadata and controls
41 lines (38 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// @ts-ignore - markdownlint-rule-helpers doesn't have TypeScript declarations
import { addError, filterTokens } from 'markdownlint-rule-helpers'
import type { RuleParams, RuleErrorCallback } from '../../types'
import { doesStringEndWithPeriod, getRange, isStringQuoted } from '../helpers/utils'
export const linkPunctuation = {
names: ['GHD001', 'link-punctuation'],
description: 'Internal link titles must not contain punctuation',
tags: ['links', 'url'],
parser: 'markdownit',
function: (params: RuleParams, onError: RuleErrorCallback) => {
filterTokens(params, 'inline', (token: any) => {
const { children, line } = token
let inLink = false
for (const child of children) {
if (child.type === 'link_open') {
inLink = true
} else if (child.type === 'link_close') {
inLink = false
} else if (inLink && child.type === 'text') {
const content = child.content.trim()
const hasPeriod = doesStringEndWithPeriod(content)
const hasQuotes = isStringQuoted(content)
if (hasPeriod || hasQuotes) {
const range = getRange(line, content)
addError(
onError,
child.lineNumber,
'Remove quotes and/or period punctuation from the link title.',
child.content,
range,
null, // No fix possible
)
}
}
}
})
},
}