Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/rules/example/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Text } from 'mdast'
import type { ValueOf } from '@/types'
import { createRule } from '@/utils'
import { getNodePosition } from '@/utils/ast'

Expand All @@ -7,7 +8,7 @@ const MESSAGE_IDS = {
exampleMsgId: 'exampleMsgId',
} as const

type MessageIds = typeof MESSAGE_IDS[keyof typeof MESSAGE_IDS]
type MessageIds = ValueOf<typeof MESSAGE_IDS>
type Options = []

export default createRule<Options, MessageIds>({
Expand Down
4 changes: 2 additions & 2 deletions src/rules/space-around-inline-element/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Emphasis, Image, InlineCode, Link, Strong } from 'mdast'
import type { RuleContext } from '@/types'
import type { RuleContext, ValueOf } from '@/types'
import type { InlineElement } from '@/types/inline-element'
import { createRule } from '@/utils'
import { getNodeContext, getNodePosition, isNestedInlineElement } from '@/utils/ast'
Expand All @@ -8,7 +8,7 @@ import { getSpaceContext } from '@/utils/space'

export const RULE_NAME = 'space-around-inline-element'

type MessageIds = typeof MESSAGE_IDS[keyof typeof MESSAGE_IDS]
type MessageIds = ValueOf<typeof MESSAGE_IDS>
type Options = []

const BEFORE_INLINE_ELEMENT_MESSAGE_IDS = new Set<MessageIds>([
Expand Down
7 changes: 4 additions & 3 deletions src/rules/space-around-word/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Text } from 'mdast'
import { TEXT_TYPE } from '@/types/text-tokenizer'
import type { ValueOf } from '@/types'
import { createRule } from '@/utils'
import { getNodeContextByParent } from '@/utils/ast'
import { buildTextNodeAst, isLatinWord } from '@/utils/text-tokenizer'
import { buildTextNodeAst, isLatinWord, TEXT_TYPE } from '@/utils/text-tokenizer'

export const RULE_NAME = 'space-around-word'
export const MESSAGE_IDS = {
Expand All @@ -14,7 +14,8 @@ export const MESSAGE_IDS = {
unexpectedSpaceAround: 'unexpectedSpaceAround',
} as const

type MessageIds = typeof MESSAGE_IDS[keyof typeof MESSAGE_IDS]
type MessageIds = ValueOf<typeof MESSAGE_IDS>

type Options = []

export default createRule<Options, MessageIds>({
Expand Down
3 changes: 2 additions & 1 deletion src/rules/valid-heading-anchor/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ValueOf } from '@/types'
import { createRule } from '@/utils'
import { calcAnchorPositionCompensate, getLikeAnchor, hasChinese, isStrictAnchor, normalizeAnchor } from '@/utils/anchor'
import { getNodeContext, getNodePosition } from '@/utils/ast'
Expand All @@ -9,7 +10,7 @@ const MESSAGE_IDS = {
invalidHeadingAnchor: 'invalidHeadingAnchor',
} as const
type Options = []
type MessageIds = typeof MESSAGE_IDS[keyof typeof MESSAGE_IDS]
type MessageIds = ValueOf<typeof MESSAGE_IDS>

export default createRule<Options, MessageIds>({
name: RULE_NAME,
Expand Down
2 changes: 2 additions & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ export type RuleContext<MessageIds extends string, Options extends unknown[]> =
>[0]

export type RuleListener = MarkdownRuleVisitor

export type ValueOf<T> = T[keyof T]
3 changes: 2 additions & 1 deletion src/types/inline-element.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Emphasis, Image, InlineCode, Link, Strong } from 'mdast'
import type { ValueOf } from '@/types/index'
import type { INLINE_SPACE_MESSAGE_IDS } from '@/types/inline-element'

/**
Expand All @@ -9,7 +10,7 @@ export type InlineElement = Link | Image | InlineCode | Emphasis | Strong
/**
* The allowed issue ids for inline element spacing rules.
*/
export type InlineElementSpaceIssue = typeof INLINE_SPACE_MESSAGE_IDS[keyof typeof INLINE_SPACE_MESSAGE_IDS]
export type InlineElementSpaceIssue = ValueOf<typeof INLINE_SPACE_MESSAGE_IDS>

/**
* The relative position to check for spacing.
Expand Down
18 changes: 1 addition & 17 deletions src/types/text-tokenizer.ts → src/types/text-tokenizer.d.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
import type { Text } from 'mdast'

export const TEXT_TYPE = {
'cjk': 'cjk',
'latin': 'latin',
'number': 'number',
'space': 'space',
'newline': 'newline',
'fullwidth-punctuation': 'fullwidth-punctuation',
'halfwidth-punctuation': 'halfwidth-punctuation',
'dash': 'dash',
'symbol': 'symbol',
'emoji': 'emoji',
'invisible': 'invisible',
'other': 'other',
} as const

export type TextType = typeof TEXT_TYPE[keyof typeof TEXT_TYPE]
import type { TextType } from '@/utils/text-tokenizer'

/**
* Source location point compatible with mdast position points
Expand Down
21 changes: 19 additions & 2 deletions src/utils/text-tokenizer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
import type { Text } from 'mdast'
import type { TextAst, TextPoint, TextPosition, TextToken, TextType } from '@/types/text-tokenizer'
import { TEXT_TYPE } from '@/types/text-tokenizer'
import type { ValueOf } from '@/types'
import type { TextAst, TextPoint, TextPosition, TextToken } from '@/types/text-tokenizer'
import { isDashPunctuation, isFullwidthPunctuation, isHalfwidthPunctuation } from './punctuation'

export const TEXT_TYPE = {
'cjk': 'cjk',
'latin': 'latin',
'number': 'number',
'space': 'space',
'newline': 'newline',
'fullwidth-punctuation': 'fullwidth-punctuation',
'halfwidth-punctuation': 'halfwidth-punctuation',
'dash': 'dash',
'symbol': 'symbol',
'emoji': 'emoji',
'invisible': 'invisible',
'other': 'other',
} as const

export type TextType = ValueOf<typeof TEXT_TYPE>

const CJK_RE = /^\p{Script=Han}$|^\p{Script=Hiragana}$|^\p{Script=Katakana}$|^\p{Script=Hangul}$/u
const LATIN_RE = /^\p{Script=Latin}$/u
const NUMBER_RE = /^\p{Number}$/u
Expand Down
Loading