Browse Source

Merge 6d381a9c47 into 3c51991c32

pull/1207/merge
Yuzhong Zhang 3 weeks ago
committed by GitHub
parent
commit
bf86783883
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 3
      src/rules_block/blockquote.ts
  2. 6
      src/rules_block/fence.ts
  3. 3
      src/rules_block/heading.ts
  4. 3
      src/rules_block/hr.ts
  5. 3
      src/rules_block/html_block.ts
  6. 3
      src/rules_block/lheading.ts
  7. 6
      src/rules_block/list.ts
  8. 5
      src/rules_block/reference.ts
  9. 10
      src/rules_block/state_block.ts
  10. 7
      src/rules_block/table.ts
  11. 40
      test/markdown-it/misc.test.mjs

3
src/rules_block/blockquote.ts

@ -9,8 +9,7 @@ export default function blockquote (state: StateBlock, startLine: number, endLin
const oldLineMax = state.lineMax
// if it's indented more than 3 spaces, it should be a code block
if (state.sCount[startLine] - state.blkIndent >= 4) { return false }
if (state.isCodeBlock(startLine)) { return false }
// check the block quote marker
if (state.src.charCodeAt(pos) !== 0x3E/* > */) { return false }

6
src/rules_block/fence.ts

@ -6,8 +6,7 @@ export default function fence (state: StateBlock, startLine: number, endLine: nu
let pos = state.bMarks[startLine] + state.tShift[startLine]
let 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 }
if (state.isCodeBlock(startLine)) { return false }
if (pos + 3 > max) { return false }
@ -61,8 +60,7 @@ export default function fence (state: StateBlock, startLine: number, endLine: nu
if (state.src.charCodeAt(pos) !== marker) { continue }
if (state.sCount[nextLine] - state.blkIndent >= 4) {
// closing fence should be indented less than 4 spaces
if (state.isCodeBlock(nextLine)) {
continue
}

3
src/rules_block/heading.ts

@ -7,8 +7,7 @@ export default function heading (state: StateBlock, startLine: number, endLine:
let pos = state.bMarks[startLine] + state.tShift[startLine]
let 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 }
if (state.isCodeBlock(startLine)) { return false }
let ch = state.src.charCodeAt(pos)

3
src/rules_block/hr.ts

@ -5,8 +5,7 @@ import type StateBlock from './state_block.ts'
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 }
if (state.isCodeBlock(startLine)) { return false }
let pos = state.bMarks[startLine] + state.tShift[startLine]
const marker = state.src.charCodeAt(pos++)

3
src/rules_block/html_block.ts

@ -25,8 +25,7 @@ export default function html_block (state: StateBlock, startLine: number, endLin
let pos = state.bMarks[startLine] + state.tShift[startLine]
let 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 }
if (state.isCodeBlock(startLine)) { return false }
if (!state.md.options.html) { return false }

3
src/rules_block/lheading.ts

@ -6,8 +6,7 @@ import type StateBlock from './state_block.ts'
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
if (state.sCount[startLine] - state.blkIndent >= 4) { return false }
if (state.isCodeBlock(startLine)) { return false }
const oldParentType = state.parentType
state.parentType = 'paragraph' // use paragraph to match terminatorRules

6
src/rules_block/list.ts

@ -93,8 +93,7 @@ export default function list (state: StateBlock, startLine: number, endLine: num
let nextLine = startLine
let tight = true
// if it's indented more than 3 spaces, it should be a code block
if (state.sCount[nextLine] - state.blkIndent >= 4) { return false }
if (state.isCodeBlock(nextLine)) { return false }
// Special case:
// - item 1
@ -284,8 +283,7 @@ export default function list (state: StateBlock, startLine: number, endLine: num
//
if (state.sCount[nextLine] < state.blkIndent) { break }
// if it's indented more than 3 spaces, it should be a code block
if (state.sCount[nextLine] - state.blkIndent >= 4) { break }
if (state.isCodeBlock(nextLine)) { break }
// fail if terminating block found
let terminate = false

5
src/rules_block/reference.ts

@ -6,8 +6,7 @@ export default function reference (state: StateBlock, startLine: number, _endLin
let max = state.eMarks[startLine]
let nextLine = startLine + 1
// if it's indented more than 3 spaces, it should be a code block
if (state.sCount[startLine] - state.blkIndent >= 4) { return false }
if (state.isCodeBlock(startLine)) { return false }
if (state.src.charCodeAt(pos) !== 0x5B/* [ */) { return false }
@ -23,7 +22,7 @@ export default function reference (state: StateBlock, startLine: number, _endLin
// this would be a code block normally, but after paragraph
// it's considered a lazy continuation regardless of what's there
if (state.sCount[nextLine] - state.blkIndent > 3) { isContinuation = true }
if (state.isCodeBlock(nextLine)) { isContinuation = true }
// quirk for blockquotes, this line should already be checked by that rule
if (state.sCount[nextLine] < 0) { isContinuation = true }

10
src/rules_block/state_block.ts

@ -42,6 +42,7 @@ class StateBlock {
parentType = 'root'
level = 0
_codeEnabled = false
// re-export Token class to use in block rules
Token = Token
@ -105,6 +106,9 @@ class StateBlock {
this.bsCount.push(0)
this.lineMax = this.bMarks.length - 1 // don't count last fake line
// pre-check if code blocks are enabled, to speed up isCodeBlock
this._codeEnabled = this.md.block.ruler.__rules__.some(rule => rule.name === 'code' && rule.enabled)
}
// Push new token to "stream".
@ -222,6 +226,12 @@ class StateBlock {
return queue.join('')
}
// Check if line is a code block, i.e. the code block rule is enabled
// and text is indented by more than 3 spaces.
isCodeBlock (line: number): boolean {
return this._codeEnabled && (this.sCount[line] - this.blkIndent) >= 4
}
}
export default StateBlock

7
src/rules_block/table.ts

@ -61,8 +61,7 @@ export default function table (state: StateBlock, startLine: number, endLine: nu
if (state.sCount[nextLine] < state.blkIndent) { return false }
// if it's indented more than 3 spaces, it should be a code block
if (state.sCount[nextLine] - state.blkIndent >= 4) { return false }
if (state.isCodeBlock(nextLine)) { return false }
// first character of the second line should be '|', '-', ':',
// and no other characters are allowed but spaces;
@ -120,7 +119,7 @@ export default function table (state: StateBlock, startLine: number, endLine: nu
lineText = getLine(state, startLine).trim()
if (lineText.indexOf('|') === -1) { return false }
if (state.sCount[startLine] - state.blkIndent >= 4) { return false }
if (state.isCodeBlock(startLine)) { return false }
columns = escapedSplit(lineText)
if (columns.length && columns[0] === '') columns.shift()
if (columns.length && columns[columns.length - 1] === '') columns.pop()
@ -182,7 +181,7 @@ export default function table (state: StateBlock, startLine: number, endLine: nu
if (terminate) { break }
lineText = getLine(state, nextLine).trim()
if (!lineText) { break }
if (state.sCount[nextLine] - state.blkIndent >= 4) { break }
if (state.isCodeBlock(nextLine)) { break }
columns = escapedSplit(lineText)
if (columns.length && columns[0] === '') columns.shift()
if (columns.length && columns[columns.length - 1] === '') columns.pop()

40
test/markdown-it/misc.test.mjs

@ -564,3 +564,43 @@ describe('Token attributes', function () {
assert.strictEqual(t.attrGet('myattr'), 'myvalue')
})
})
describe('disable indented code', function () {
const md = markdownit().disable('code')
it('should parse a 4-space indented heading', function () {
assert.strictEqual(
md.render(' # Heading'),
'<h1>Heading</h1>\n'
)
})
it('should parse a 4-space indented blockquote', function () {
assert.strictEqual(
md.render(' > quote'),
'<blockquote>\n<p>quote</p>\n</blockquote>\n'
)
})
it('should parse a 4-space indented hr', function () {
assert.strictEqual(
md.render(' ---'),
'<hr>\n'
)
})
it('should parse a 4-space indented fence', function () {
assert.strictEqual(
md.render(' ```\n foo\n ```'),
'<pre><code>foo\n</code></pre>\n'
)
})
it('should keep 4-space indented heading as a code block when code is enabled', function () {
const mdDefault = markdownit()
assert.strictEqual(
mdDefault.render(' # Heading'),
'<pre><code># Heading\n</code></pre>\n'
)
})
})

Loading…
Cancel
Save