Skip to content

Commit

Permalink
Complete test coverage for markdown-it
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Dec 8, 2023
1 parent 7a198a8 commit 260c08a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/markdown-it/deflist.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Definition list
* Render a definition list
*
* @param {Function} md - markdown-it instance
* @returns {Function} - markdown-it rendering rules
Expand Down
2 changes: 1 addition & 1 deletion lib/markdown-it/footnote.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Footnotes
* Render footnotes
*
* @param {Function} md - markdown-it instance
* @returns {Function} - markdown-it rendering rules
Expand Down
2 changes: 1 addition & 1 deletion lib/markdown-it/table-of-contents.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Table of contents
* Render a table of contents
*
* @param {Function} md - markdown-it instance
* @returns {Function} - markdown-it rendering rules
Expand Down
4 changes: 1 addition & 3 deletions lib/markdown-it/table.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/**
* Responsive tables
*
* Add `tabindex` so that table scrolling can be controlled via the keyboard.
* Render a table with `tabindex` to enable keyboard scrolling
*
* @param {Function} md - markdown-it instance
* @returns {Function} - markdown-it rendering rules
Expand Down
56 changes: 56 additions & 0 deletions test/lib/markdown-it.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { strict as assert } from 'node:assert'
import { describe, it } from 'node:test'
import md from '../../lib/markdown-it.js'

describe('markdown-it', () => {
it('Returns configured markdown-it parser', () => {
const { options } = md()

assert.equal(options.breaks, true)
assert.equal(typeof options.highlight, 'function')
assert.equal(options.html, true)
assert.equal(options.linkify, false)
assert.equal(options.typographer, true)
})

it('Renders anchor heading permalinks when option enabled', () => {
const result = md({
headingPermalinks: true
}).render('# Heading')

assert.equal(result, '<h1 id="heading" tabindex="-1" class="govuk-heading-xl"><a class="app-link--heading govuk-link" href="#heading"><span>Heading</span></a></h1>\n')
})

it('Renders a definition list', () => {
const result = md().render('Term\n: Definition')

assert.match(result, /<dl class="app-definition-list"/)
})

it('Renders footnotes', () => {
const result = md().render('A[^1]\nB[^1]\n\n[^1]: One')

assert.match(result, /<sup id="fnref:1"/)
assert.match(result, /href="#fn:1"/)
assert.match(result, /<li id="fn:1"/)
assert.match(result, /href="#fnref:1"/)

assert.match(result, /<sup id="fnref:1:1"/)
assert.match(result, /href="#fn:1:1"/)
})

it('Renders a table of contents', () => {
const result = md().render('[[toc]]\n\n# A\n\n## B')

assert.match(result, /<nav class="app-contents-list"/)
assert.match(result, /aria-label="Contents"/)
assert.match(result, /href="#b"/)
assert.doesNotMatch(result, /href="#a"/)
})

it('Renders a table with `tabindex` to enable keyboard scrolling', () => {
const result = md().render('|A|B|\n|-|-|\n|1|2|')

assert.match(result, /<table tabindex="0"/)
})
})

0 comments on commit 260c08a

Please sign in to comment.