Browse Source

Migrate to TS (pass 4, rules)

pull/1197/head
Vitaly Puzrin 3 months ago
parent
commit
003b77a421
  1. 3
      src/rules_block/blockquote.ts
  2. 4
      src/rules_block/code.ts
  3. 4
      src/rules_block/fence.ts
  4. 3
      src/rules_block/heading.ts
  5. 3
      src/rules_block/hr.ts
  6. 3
      src/rules_block/html_block.ts
  7. 3
      src/rules_block/lheading.ts
  8. 3
      src/rules_block/list.ts
  9. 3
      src/rules_block/paragraph.ts
  10. 3
      src/rules_block/reference.ts
  11. 3
      src/rules_block/table.ts
  12. 4
      src/rules_core/block.ts
  13. 4
      src/rules_core/inline.ts
  14. 3
      src/rules_core/linkify.ts
  15. 4
      src/rules_core/normalize.ts
  16. 4
      src/rules_core/replacements.ts
  17. 3
      src/rules_core/smartquotes.ts
  18. 4
      src/rules_core/text_join.ts
  19. 4
      src/rules_inline/autolink.ts
  20. 4
      src/rules_inline/backticks.ts
  21. 4
      src/rules_inline/balance_pairs.ts
  22. 6
      src/rules_inline/emphasis.ts
  23. 3
      src/rules_inline/entity.ts
  24. 3
      src/rules_inline/escape.ts
  25. 4
      src/rules_inline/fragments_join.ts
  26. 3
      src/rules_inline/html_inline.ts
  27. 3
      src/rules_inline/image.ts
  28. 3
      src/rules_inline/link.ts
  29. 4
      src/rules_inline/linkify.ts
  30. 3
      src/rules_inline/newline.ts
  31. 6
      src/rules_inline/strikethrough.ts
  32. 4
      src/rules_inline/text.ts

3
src/rules_block/blockquote.ts

@ -1,8 +1,9 @@
// Block quotes
import { isSpace } from '../common/utils.ts'
import type StateBlock from './state_block.ts'
export default function blockquote (state, startLine, endLine, silent) {
export default function blockquote (state: StateBlock, startLine: number, endLine: number, silent: boolean): boolean {
let pos = state.bMarks[startLine] + state.tShift[startLine]
let max = state.eMarks[startLine]

4
src/rules_block/code.ts

@ -1,6 +1,8 @@
// Code block (4 spaces padded)
export default function code (state, startLine, endLine/*, silent */) {
import type StateBlock from './state_block.ts'
export default function code (state: StateBlock, startLine: number, endLine: number/*, silent */): boolean {
if (state.sCount[startLine] - state.blkIndent < 4) { return false }
let nextLine = startLine + 1

4
src/rules_block/fence.ts

@ -1,6 +1,8 @@
// fences (``` lang, ~~~ lang)
export default function fence (state, startLine, endLine, silent) {
import type StateBlock from './state_block.ts'
export default function fence (state: StateBlock, startLine: number, endLine: number, silent: boolean): boolean {
let pos = state.bMarks[startLine] + state.tShift[startLine]
let max = state.eMarks[startLine]

3
src/rules_block/heading.ts

@ -1,8 +1,9 @@
// heading (#, ##, ...)
import { isSpace, asciiTrim } from '../common/utils.ts'
import type StateBlock from './state_block.ts'
export default function heading (state, startLine, endLine, silent) {
export default function heading (state: StateBlock, startLine: number, endLine: number, silent: boolean): boolean {
let pos = state.bMarks[startLine] + state.tShift[startLine]
let max = state.eMarks[startLine]

3
src/rules_block/hr.ts

@ -1,8 +1,9 @@
// Horizontal rule
import { isSpace } from '../common/utils.ts'
import type StateBlock from './state_block.ts'
export default function hr (state, startLine, endLine, silent) {
export default function hr (state: StateBlock, startLine: number, endLine: number, silent: boolean): boolean {
const max = state.eMarks[startLine]
// if it's indented more than 3 spaces, it should be a code block
if (state.sCount[startLine] - state.blkIndent >= 4) { return false }

3
src/rules_block/html_block.ts

@ -2,6 +2,7 @@
import block_names from '../common/html_blocks.ts'
import { HTML_OPEN_CLOSE_TAG_RE } from '../common/html_re.ts'
import type StateBlock from './state_block.ts'
// An array of opening and corresponding closing sequences for html tags,
// last argument defines whether it can terminate a paragraph or not
@ -20,7 +21,7 @@ const HTML_SEQUENCES: Array<[
[new RegExp(HTML_OPEN_CLOSE_TAG_RE.source + '\\s*$'), /^$/, false]
]
export default function html_block (state, startLine, endLine, silent) {
export default function html_block (state: StateBlock, startLine: number, endLine: number, silent: boolean): boolean {
let pos = state.bMarks[startLine] + state.tShift[startLine]
let max = state.eMarks[startLine]

3
src/rules_block/lheading.ts

@ -1,8 +1,9 @@
// lheading (---, ===)
import { asciiTrim } from '../common/utils.ts'
import type StateBlock from './state_block.ts'
export default function lheading (state, startLine, endLine/*, silent */) {
export default function lheading (state: StateBlock, startLine: number, endLine: number/*, silent */): boolean {
const terminatorRules = state.md.block.ruler.getRules('paragraph')
// if it's indented more than 3 spaces, it should be a code block

3
src/rules_block/list.ts

@ -1,6 +1,7 @@
// Lists
import { isSpace } from '../common/utils.ts'
import type StateBlock from './state_block.ts'
// Search `[-+*][\n ]`, returns next pos after marker on success
// or -1 on fail.
@ -87,7 +88,7 @@ function markTightParagraphs (state, idx) {
}
}
export default function list (state, startLine, endLine, silent) {
export default function list (state: StateBlock, startLine: number, endLine: number, silent: boolean): boolean {
let max, pos, start, token
let nextLine = startLine
let tight = true

3
src/rules_block/paragraph.ts

@ -1,8 +1,9 @@
// Paragraph
import { asciiTrim } from '../common/utils.ts'
import type StateBlock from './state_block.ts'
export default function paragraph (state, startLine, endLine) {
export default function paragraph (state: StateBlock, startLine: number, endLine: number): boolean {
const terminatorRules = state.md.block.ruler.getRules('paragraph')
const oldParentType = state.parentType
let nextLine = startLine + 1

3
src/rules_block/reference.ts

@ -1,6 +1,7 @@
import { isSpace, normalizeReference } from '../common/utils.ts'
import type StateBlock from './state_block.ts'
export default function reference (state, startLine, _endLine, silent) {
export default function reference (state: StateBlock, startLine: number, _endLine: number, silent: boolean): boolean {
let pos = state.bMarks[startLine] + state.tShift[startLine]
let max = state.eMarks[startLine]
let nextLine = startLine + 1

3
src/rules_block/table.ts

@ -1,6 +1,7 @@
// GFM table, https://github.github.com/gfm/#tables-extension-
import { isSpace } from '../common/utils.ts'
import type StateBlock from './state_block.ts'
// Limit the amount of empty autocompleted cells in a table,
// see https://github.com/markdown-it/markdown-it/issues/1000,
@ -52,7 +53,7 @@ function escapedSplit (str) {
return result
}
export default function table (state, startLine, endLine, silent) {
export default function table (state: StateBlock, startLine: number, endLine: number, silent: boolean): boolean {
// should have at least two lines
if (startLine + 2 > endLine) { return false }

4
src/rules_core/block.ts

@ -1,4 +1,6 @@
export default function block (state) {
import type StateCore from './state_core.ts'
export default function block (state: StateCore): void {
let token
if (state.inlineMode) {

4
src/rules_core/inline.ts

@ -1,4 +1,6 @@
export default function inline (state) {
import type StateCore from './state_core.ts'
export default function inline (state: StateCore): void {
const tokens = state.tokens
// Parse inlines

3
src/rules_core/linkify.ts

@ -4,6 +4,7 @@
//
import { arrayReplaceAt } from '../common/utils.ts'
import type StateCore from './state_core.ts'
function isLinkOpen (str) {
return /^<a[>\s]/i.test(str)
@ -12,7 +13,7 @@ function isLinkClose (str) {
return /^<\/a\s*>/i.test(str)
}
export default function linkify (state) {
export default function linkify (state: StateCore): void {
const blockTokens = state.tokens
if (!state.md.options.linkify) { return }

4
src/rules_core/normalize.ts

@ -1,10 +1,12 @@
// Normalize input string
import type StateCore from './state_core.ts'
// https://spec.commonmark.org/0.29/#line-ending
const NEWLINES_RE = /\r\n?|\n/g
const NULL_RE = /\0/g
export default function normalize (state) {
export default function normalize (state: StateCore): void {
let str
// Normalize newlines

4
src/rules_core/replacements.ts

@ -13,6 +13,8 @@
// - fractionals 1/2, 1/4, 3/4 -> ½, ¼, ¾
// - multiplications 2 x 4 -> 2 × 4
import type StateCore from './state_core.ts'
const RARE_RE = /\+-|\.\.|\?\?\?\?|!!!!|,,|--/
// Workaround for phantomjs - need regex without /g flag,
@ -82,7 +84,7 @@ function replace_rare (inlineTokens) {
}
}
export default function replace (state) {
export default function replace (state: StateCore): void {
let blkIdx
if (!state.md.options.typographer) { return }

3
src/rules_core/smartquotes.ts

@ -2,6 +2,7 @@
//
import { isWhiteSpace, isPunctCharCode, isMdAsciiPunct } from '../common/utils.ts'
import type StateCore from './state_core.ts'
const QUOTE_TEST_RE = /['"]/
const QUOTE_RE = /['"]/g
@ -194,7 +195,7 @@ function process_inlines (tokens, state) {
})
}
export default function smartquotes (state) {
export default function smartquotes (state: StateCore): void {
/* eslint max-depth:0 */
if (!state.md.options.typographer) { return }

4
src/rules_core/text_join.ts

@ -6,7 +6,9 @@
// For example, `\:)` shouldn't be replaced with an emoji.
//
export default function text_join (state) {
import type StateCore from './state_core.ts'
export default function text_join (state: StateCore): void {
let curr, last
const blockTokens = state.tokens
const l = blockTokens.length

4
src/rules_inline/autolink.ts

@ -1,11 +1,13 @@
// Process autolinks '<protocol:...>'
import type StateInline from './state_inline.ts'
/* eslint max-len:0 */
const EMAIL_RE = /^([a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*)$/
/* eslint-disable-next-line no-control-regex */
const AUTOLINK_RE = /^([a-zA-Z][a-zA-Z0-9+.-]{1,31}):([^<>\x00-\x20]*)$/
export default function autolink (state, silent) {
export default function autolink (state: StateInline, silent: boolean): boolean {
let pos = state.pos
if (state.src.charCodeAt(pos) !== 0x3C/* < */) { return false }

4
src/rules_inline/backticks.ts

@ -1,6 +1,8 @@
// Parse backticks
export default function backtick (state, silent) {
import type StateInline from './state_inline.ts'
export default function backtick (state: StateInline, silent: boolean): boolean {
let pos = state.pos
const ch = state.src.charCodeAt(pos)

4
src/rules_inline/balance_pairs.ts

@ -1,6 +1,8 @@
// For each opening emphasis-like marker find a matching closing one
//
import type StateInline from './state_inline.ts'
function processDelimiters (delimiters) {
const openersBottom = {}
const max = delimiters.length
@ -110,7 +112,7 @@ function processDelimiters (delimiters) {
}
}
export default function link_pairs (state) {
export default function link_pairs (state: StateInline): void {
const tokens_meta = state.tokens_meta
const max = state.tokens_meta.length

6
src/rules_inline/emphasis.ts

@ -1,9 +1,11 @@
// Process *this* and _that_
//
import type StateInline from './state_inline.ts'
// Insert each marker as a separate text token, and add it to delimiter list
//
function emphasis_tokenize (state, silent) {
function emphasis_tokenize (state: StateInline, silent: boolean): boolean {
const start = state.pos
const marker = state.src.charCodeAt(start)
@ -104,7 +106,7 @@ function postProcess (state, delimiters) {
// Walk through delimiter list and replace text tokens with tags
//
function emphasis_post_process (state) {
function emphasis_post_process (state: StateInline): void {
const tokens_meta = state.tokens_meta
const max = state.tokens_meta.length

3
src/rules_inline/entity.ts

@ -2,11 +2,12 @@
import { decodeHTMLStrict } from 'entities'
import { isValidEntityCode, fromCodePoint } from '../common/utils.ts'
import type StateInline from './state_inline.ts'
const DIGITAL_RE = /^&#((?:x[a-f0-9]{1,6}|[0-9]{1,7}));/i
const NAMED_RE = /^&([a-z][a-z0-9]{1,31});/i
export default function entity (state, silent) {
export default function entity (state: StateInline, silent: boolean): boolean {
const pos = state.pos
const max = state.posMax

3
src/rules_inline/escape.ts

@ -1,6 +1,7 @@
// Process escaped chars and hardbreaks
import { isSpace } from '../common/utils.ts'
import type StateInline from './state_inline.ts'
const ESCAPED = []
@ -9,7 +10,7 @@ for (let i = 0; i < 256; i++) { ESCAPED.push(0) }
'\\!"#$%&\'()*+,./:;<=>?@[]^_`{|}~-'
.split('').forEach(function (ch) { ESCAPED[ch.charCodeAt(0)] = 1 })
export default function escape (state, silent) {
export default function escape (state: StateInline, silent: boolean): boolean {
let pos = state.pos
const max = state.posMax

4
src/rules_inline/fragments_join.ts

@ -7,7 +7,9 @@
// into opening/closing tags (which messes up levels inside).
//
export default function fragments_join (state) {
import type StateInline from './state_inline.ts'
export default function fragments_join (state: StateInline): void {
let curr, last
let level = 0
const tokens = state.tokens

3
src/rules_inline/html_inline.ts

@ -1,6 +1,7 @@
// Process html tags
import { HTML_TAG_RE } from '../common/html_re.ts'
import type StateInline from './state_inline.ts'
function isLinkOpen (str) {
return /^<a[>\s]/i.test(str)
@ -15,7 +16,7 @@ function isLetter (ch) {
return (lc >= 0x61/* a */) && (lc <= 0x7a/* z */)
}
export default function html_inline (state, silent) {
export default function html_inline (state: StateInline, silent: boolean): boolean {
if (!state.md.options.html) { return false }
// Check start

3
src/rules_inline/image.ts

@ -1,8 +1,9 @@
// Process ![image](<src> "title")
import { normalizeReference, isSpace } from '../common/utils.ts'
import type StateInline from './state_inline.ts'
export default function image (state, silent) {
export default function image (state: StateInline, silent: boolean): boolean {
let code, content, label, pos, ref, res, title, start
let href = ''
const oldPos = state.pos

3
src/rules_inline/link.ts

@ -1,8 +1,9 @@
// Process [link](<to> "stuff")
import { normalizeReference, isSpace } from '../common/utils.ts'
import type StateInline from './state_inline.ts'
export default function link (state, silent) {
export default function link (state: StateInline, silent: boolean): boolean {
let code, label, res, ref
let href = ''
let title = ''

4
src/rules_inline/linkify.ts

@ -1,9 +1,11 @@
// Process links like https://example.org/
import type StateInline from './state_inline.ts'
// RFC3986: scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
const SCHEME_RE = /(?:^|[^a-z0-9.+-])([a-z][a-z0-9.+-]*)$/i
export default function linkify (state, silent) {
export default function linkify (state: StateInline, silent: boolean): boolean {
if (!state.md.options.linkify) return false
if (state.linkLevel > 0) return false

3
src/rules_inline/newline.ts

@ -1,8 +1,9 @@
// Proceess '\n'
import { isSpace } from '../common/utils.ts'
import type StateInline from './state_inline.ts'
export default function newline (state, silent) {
export default function newline (state: StateInline, silent: boolean): boolean {
let pos = state.pos
if (state.src.charCodeAt(pos) !== 0x0A/* \n */) { return false }

6
src/rules_inline/strikethrough.ts

@ -1,9 +1,11 @@
// ~~strike through~~
//
import type StateInline from './state_inline.ts'
// Insert each marker as a separate text token, and add it to delimiter list
//
function strikethrough_tokenize (state, silent) {
function strikethrough_tokenize (state: StateInline, silent: boolean): boolean {
const start = state.pos
const marker = state.src.charCodeAt(start)
@ -108,7 +110,7 @@ function postProcess (state, delimiters) {
// Walk through delimiter list and replace text tokens with tags
//
function strikethrough_postProcess (state) {
function strikethrough_postProcess (state: StateInline): void {
const tokens_meta = state.tokens_meta
const max = state.tokens_meta.length

4
src/rules_inline/text.ts

@ -1,6 +1,8 @@
// Skip text characters for text token, place those to pending buffer
// and increment current pos
import type StateInline from './state_inline.ts'
// Rule to skip pure text
// '{}$%@~+=:' reserved for extentions
@ -39,7 +41,7 @@ function isTerminatorChar (ch) {
}
}
export default function text (state, silent) {
export default function text (state: StateInline, silent: boolean): boolean {
let pos = state.pos
while (pos < state.posMax && !isTerminatorChar(state.src.charCodeAt(pos))) {

Loading…
Cancel
Save