-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
148 lines (128 loc) · 4.66 KB
/
index.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* Get default renderer for given markdown-it rule
*
* @param {import('markdown-it')} md - markdown-it instance
* @param {string} rule - Rule to modify
* @returns {Function} - Renderer for the given rule
*/
const getDefaultRenderer = (md, rule) => {
return (
md.renderer.rules[rule] ||
function (tokens, idx, options, env, self) {
return self.renderToken(tokens, idx, options)
}
)
}
/**
* Add classes to a token’s class attribute in given markdown-it rule
*
* @param {import('markdown-it')} md - markdown-it instance
* @param {string} rule - Rule to modify
* @param {string} classes - Classes to add to the rule’s token
*/
const addClassesToRule = (md, rule, classes) => {
const defaultRenderer = getDefaultRenderer(md, rule)
md.renderer.rules[rule] = (tokens, idx, options, env, self) => {
const token = tokens[idx]
if (token.attrGet('class')) {
token.attrJoin('class', classes)
} else {
token.attrPush(['class', classes])
}
return defaultRenderer(tokens, idx, options, env, self)
}
}
const defaultOptions = {
headingsStartWith: 'l',
calvert: false
}
/**
* Adds GOV.UK typography classes to blockquotes, headings, paragraphs, links,
* lists, section breaks and tables and updates references to local files in
* links and images to friendly URLs
*
* @param {import('markdown-it')} md - markdown-it instance
* @param {object} pluginOptions - Plugin options
*/
module.exports = function plugin(md, pluginOptions = {}) {
// Merge options
pluginOptions = { ...defaultOptions, ...pluginOptions }
// Headings
const headingRenderer = getDefaultRenderer(md, 'heading_open')
md.renderer.rules.heading_open = (tokens, idx, options, env, self) => {
// Headings can start with either `xl` or `l` size modifier
const { headingsStartWith } = pluginOptions
const modifiers = [...(headingsStartWith === 'xl' ? ['xl'] : []), 'l', 'm']
const level = tokens[idx].tag.replace(/^h(:?\d{1}?)/, '$1')
const headingLevel = Number(level)
const modifier = modifiers[headingLevel - 1] || 's'
tokens[idx].attrPush(['class', `govuk-heading-${modifier}`])
return headingRenderer(tokens, idx, options, env, self)
}
// Block quotes
addClassesToRule(
md,
'blockquote_open',
'govuk-inset-text govuk-!-margin-left-0'
)
// Block code (indented, not fenced)
addClassesToRule(md, 'code_block', 'govuk-inset-text govuk-!-margin-left-0')
// Inline code
addClassesToRule(md, 'code_inline', 'x-govuk-code x-govuk-code--inline')
// Paragraphs
addClassesToRule(md, 'paragraph_open', 'govuk-body')
// Links
addClassesToRule(md, 'link_open', 'govuk-link')
// Lists
addClassesToRule(md, 'bullet_list_open', 'govuk-list govuk-list--bullet')
addClassesToRule(md, 'ordered_list_open', 'govuk-list govuk-list--number')
// Section break
addClassesToRule(
md,
'hr',
'govuk-section-break govuk-section-break--xl govuk-section-break--visible'
)
// Tables
addClassesToRule(md, 'table_open', 'govuk-table')
addClassesToRule(md, 'thead_open', 'govuk-table__head')
addClassesToRule(md, 'tbody_open', 'govuk-table__body')
addClassesToRule(md, 'tr_open', 'govuk-table__row')
addClassesToRule(md, 'th_open', 'govuk-table__header')
addClassesToRule(md, 'td_open', 'govuk-table__cell')
// Text replacements
const defaultTextRenderer = getDefaultRenderer(md, 'text')
md.renderer.rules.text = (tokens, idx, options, env, self) => {
const { calvert } = pluginOptions
const improveAll = !Array.isArray(calvert) && calvert === true
const improveFractions =
Array.isArray(calvert) && calvert.includes('fractions')
const improveGuillemets =
Array.isArray(calvert) && calvert.includes('guillemets')
const improveMathematical =
Array.isArray(calvert) && calvert.includes('mathematical')
// Improve fractions
if (improveAll || improveFractions) {
tokens[idx].content = tokens[idx].content
.replace(/(?<!\d)1\/2(?!\d)/g, '½')
.replace(/(?<!\d)1\/3(?!\d)/g, '⅓')
.replace(/(?<!\d)2\/3(?!\d)/g, '⅔')
.replace(/(?<!\d)1\/4(?!\d)/g, '¼')
.replace(/(?<!\d)3\/4(?!\d)/g, '¾')
}
// Improve guillemets
if (improveAll || improveGuillemets) {
tokens[idx].content = tokens[idx].content
.replace(/<</g, '«')
.replace(/>>/g, '»')
}
// Improve mathematical symbols
if (improveAll || improveMathematical) {
tokens[idx].content = tokens[idx].content
.replace(/\+-/g, '±')
.replace(/(?<= )x(?= )/g, '×')
.replace(/(?<= )<=(?= )/g, '≤')
.replace(/(?<= )=>(?= )/g, '≥')
}
return defaultTextRenderer(tokens, idx, options, env, self)
}
}