|
3 | 3 | 'use strict'; |
4 | 4 |
|
5 | 5 | const docsifyAutoHeadersDefaults = { |
6 | | - separator: '.', |
| 6 | + separator: '-', |
7 | 7 |
|
8 | 8 | levels: 6, |
9 | 9 | // levels: { start: 2, finish: 3 }, |
|
14 | 14 | debug: true |
15 | 15 | }; |
16 | 16 |
|
17 | | - const defaultErrors = { |
| 17 | + const errorMessage = { |
18 | 18 | configurationNotSetCorrectly: |
19 | 19 | 'Config settings not set', |
20 | 20 | invalidHeadingLevels: |
|
34 | 34 | misconfiguredSignifier: |
35 | 35 | 'The markdown file has the signifier, but it is misconfigured', |
36 | 36 | invalidSidebar: |
37 | | - 'The sidebar parameter needs to be a boolean - true or false' |
| 37 | + 'The sidebar parameter needs to be a boolean - true or false', |
| 38 | + exitingError: |
| 39 | + 'An error has been found in your configuration, or setup. Please review your code and markdown data' |
| 40 | + }; |
| 41 | + |
| 42 | + const showErrorMessage = (shouldShow, message) => { |
| 43 | + if (shouldShow) { throw new Error(message); } |
38 | 44 | }; |
39 | 45 |
|
40 | 46 | const setDefaultOptions = (options) => { |
41 | 47 | if (!options.separator || options.levels === undefined) { |
42 | | - console.info(defaultErrors.configurationNotSetCorrectly); |
43 | | - throw new Error(defaultErrors.configurationNotSetCorrectly); |
| 48 | + showErrorMessage(options.debug, errorMessage.configurationNotSetCorrectly); |
44 | 49 | } |
45 | 50 |
|
46 | 51 | const separatorMap = { |
|
59 | 64 | return { separator, levels, sidebar, debug }; |
60 | 65 | }; |
61 | 66 |
|
62 | | - const getUsingSidebar = (input) => { |
| 67 | + const validateAndReturnSidebar = (input) => { |
63 | 68 | if (typeof input !== 'boolean') { |
64 | | - throw new Error(defaultErrors.invalidSidebar); |
| 69 | + showErrorMessage(options.debug, errorMessage.invalidSidebar); |
65 | 70 | } else { |
66 | 71 | return input; |
67 | 72 | } |
68 | 73 | }; |
69 | 74 |
|
70 | | - const getHeadingRange = (input) => { |
| 75 | + const validateAndGetHeadingRange = (input) => { |
71 | 76 | if (typeof input !== 'number' && (typeof input !== 'object' || input === null)) { |
72 | | - throw new Error(defaultErrors.invalidHeadingLevels); |
| 77 | + showErrorMessage(options.debug, errorMessage.invalidHeadingLevels); |
73 | 78 | } |
74 | 79 |
|
75 | 80 | const isInRange = (value, min, max) => value >= min && value <= max; |
|
83 | 88 | ({ start, finish } = input); |
84 | 89 |
|
85 | 90 | if (typeof start !== 'number' || typeof finish !== 'number') { |
86 | | - throw new Error(defaultErrors.nonNumericValue); |
| 91 | + showErrorMessage(options.debug, errorMessage.nonNumericValue); |
87 | 92 | } |
88 | 93 |
|
89 | 94 | if (start > finish) { |
90 | | - throw new Error(defaultErrors.invalidHeadingLevelOrder); |
| 95 | + showErrorMessage(options.debug, errorMessage.invalidHeadingLevelOrder); |
91 | 96 | } |
92 | 97 | } |
93 | 98 |
|
94 | 99 | if (!isInRange(start, 1, 6) || !isInRange(finish, 1, 6)) { |
95 | | - throw new Error(defaultErrors.headingLevelRange); |
| 100 | + showErrorMessage(options.debug, errorMessage.headingLevelRange); |
96 | 101 | } |
97 | 102 |
|
98 | 103 | const headings = {}; |
|
112 | 117 | const isAlphabetic = elements.every(isAllAlphabetic); |
113 | 118 |
|
114 | 119 | if (!(isNumeric || isAlphabetic)) { |
115 | | - throw new Error(defaultErrors.invalidHeadingLevels); |
| 120 | + showErrorMessage(options.debug, errorMessage.invalidHeadingLevels); |
116 | 121 | } |
117 | 122 |
|
118 | 123 | while (elements.length < 6) { |
|
130 | 135 | const checkAutoHeader = (markdown) => { |
131 | 136 | const autoHeaderPattern = /^(?:@autoHeader:|<!-- autoHeader:)([\d.a-zA-Z\-:,~]+)(?: -->)?/; |
132 | 137 | const match = markdown.trim().match(autoHeaderPattern); |
133 | | - return match ? match[1] : null; |
| 138 | + if (!match) { |
| 139 | + showErrorMessage(options.debug, errorMessage.missingAutoHeaderSignifier); |
| 140 | + } |
| 141 | + return match[1]; |
134 | 142 | }; |
135 | 143 |
|
136 | | - |
137 | 144 | const createCountContextObjects = (levels) => { |
138 | 145 | const configEntries = Object.entries(levels); |
139 | 146 | const currentCounts = new Map( |
|
152 | 159 | }; |
153 | 160 | }; |
154 | 161 |
|
155 | | - const applyCurrentCountThroughBoundContext = function (headingNode, options) { |
| 162 | + const applyCurrentCountThroughBoundContext = (headingNode, options) => { |
156 | 163 | const { currentCounts, scopedTagNames } = this; |
157 | 164 | const headingName = headingNode.tagName.toLowerCase(); |
158 | 165 | const headingNameList = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']; |
|
241 | 248 | const defaultOptions = setDefaultOptions(docsifyAutoHeadersDefaults); |
242 | 249 | let options = { |
243 | 250 | separator: defaultOptions.separator, |
244 | | - levels: getHeadingRange(defaultOptions.levels), |
245 | | - sidebar: getUsingSidebar(defaultOptions.scope), |
246 | | - debug: defaultOptions.debug |
| 251 | + levels: validateAndGetHeadingRange(defaultOptions.levels), |
| 252 | + sidebar: validateAndReturnSidebar(defaultOptions.sidebar), |
| 253 | + debug: defaultOptions.debug, |
| 254 | + shouldContinue: false |
247 | 255 | }; |
248 | 256 |
|
249 | 257 | // MARK: Need to access markdown to extract the signifier data |
250 | 258 | hook.beforeEach((markdown, next) => { |
| 259 | + let cleanedMarkdown; |
251 | 260 | try { |
252 | 261 | const headingSignifier = checkAutoHeader(markdown); |
253 | 262 | if (!headingSignifier) { |
254 | | - throw new Error(defaultErrors.missingAutoHeaderSignifier); |
| 263 | + options.shouldContinue = false; |
| 264 | + showErrorMessage(options.debug, errorMessage.missingAutoHeaderSignifier); |
255 | 265 | } |
256 | 266 |
|
257 | | - const headingRanges = getHeadingRange(defaultOptions.levels); |
| 267 | + const headingRanges = validateAndGetHeadingRange(defaultOptions.levels); |
258 | 268 | const startingHeadingValues = getStartingValues( |
259 | 269 | headingSignifier, |
260 | 270 | options.separator |
261 | 271 | ); |
262 | 272 | if (!startingHeadingValues) { |
263 | | - throw new Error(defaultErrors.misconfiguredSignifier); |
| 273 | + options.shouldContinue = false; |
| 274 | + showErrorMessage(options.debug, errorMessage.misconfiguredSignifier); |
264 | 275 | } |
265 | 276 | const headingConfiguration = {}; |
266 | 277 | for (const key in headingRanges) { |
|
272 | 283 |
|
273 | 284 | // Update the options for use elsewhere |
274 | 285 | options.levels = headingConfiguration; |
| 286 | + options.shouldContinue = true; |
275 | 287 |
|
276 | | - const cleanedMarkdown = markdown.split('\n').slice(1).join('\n'); |
277 | | - return cleanedMarkdown; |
278 | | - |
| 288 | + cleanedMarkdown = markdown.split('\n').slice(1).join('\n'); |
279 | 289 | } catch (error) { |
| 290 | + options.shouldContinue = false; |
280 | 291 | console.warn('Warning: Docsify Auto Headers\n', error.message); |
281 | 292 | } finally { |
282 | | - next(markdown); |
| 293 | + next(cleanedMarkdown); |
283 | 294 | } |
284 | 295 | }); |
285 | 296 |
|
286 | 297 | // Conditional setup for hooks |
287 | 298 | if (options.sidebar) { |
288 | | - |
289 | | - |
290 | 299 | hook.beforeEach((markdown, next) => { |
| 300 | + if (!options.shouldContinue) { |
| 301 | + showErrorMessage(options.debug, errorMessage.exitingError); |
| 302 | + } |
| 303 | + |
291 | 304 | let output; |
292 | 305 | try { |
293 | 306 | output = applyScopedHeadingCounts( |
|
297 | 310 | 'markdown' |
298 | 311 | ); |
299 | 312 | } catch (error) { |
300 | | - console.warn('Warning: Docsify Auto Headers\n', error.message) |
| 313 | + console.warn('Warning: Docsify Auto Headers - beforeEach 2\n', error.message); |
301 | 314 | } finally { |
302 | 315 | next(output); |
303 | 316 | } |
304 | 317 | }); |
| 318 | + |
305 | 319 | } else { |
306 | 320 | hook.afterEach(function (html, next) { |
| 321 | + if (!options.shouldContinue) { |
| 322 | + showErrorMessage(options.debug, errorMessage.exitingError); |
| 323 | + } |
| 324 | + |
307 | 325 | let output; |
308 | 326 | try { |
309 | 327 | output = applyScopedHeadingCounts( |
|
313 | 331 | 'html' |
314 | 332 | ); |
315 | 333 | } catch (error) { |
316 | | - console.warn('Warning: Docsify Auto Headers\n', error.message) |
| 334 | + console.warn('Warning: Docsify Auto Headers\n', error.message); |
317 | 335 | } finally { |
318 | 336 | next(output); |
319 | 337 | } |
|
0 commit comments