Skip to content

Commit f9a6c22

Browse files
.
1 parent 5718f7b commit f9a6c22

File tree

1 file changed

+48
-30
lines changed

1 file changed

+48
-30
lines changed

docs/docsify-auto-headers.js

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
'use strict';
44

55
const docsifyAutoHeadersDefaults = {
6-
separator: '.',
6+
separator: '-',
77

88
levels: 6,
99
// levels: { start: 2, finish: 3 },
@@ -14,7 +14,7 @@
1414
debug: true
1515
};
1616

17-
const defaultErrors = {
17+
const errorMessage = {
1818
configurationNotSetCorrectly:
1919
'Config settings not set',
2020
invalidHeadingLevels:
@@ -34,13 +34,18 @@
3434
misconfiguredSignifier:
3535
'The markdown file has the signifier, but it is misconfigured',
3636
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); }
3844
};
3945

4046
const setDefaultOptions = (options) => {
4147
if (!options.separator || options.levels === undefined) {
42-
console.info(defaultErrors.configurationNotSetCorrectly);
43-
throw new Error(defaultErrors.configurationNotSetCorrectly);
48+
showErrorMessage(options.debug, errorMessage.configurationNotSetCorrectly);
4449
}
4550

4651
const separatorMap = {
@@ -59,17 +64,17 @@
5964
return { separator, levels, sidebar, debug };
6065
};
6166

62-
const getUsingSidebar = (input) => {
67+
const validateAndReturnSidebar = (input) => {
6368
if (typeof input !== 'boolean') {
64-
throw new Error(defaultErrors.invalidSidebar);
69+
showErrorMessage(options.debug, errorMessage.invalidSidebar);
6570
} else {
6671
return input;
6772
}
6873
};
6974

70-
const getHeadingRange = (input) => {
75+
const validateAndGetHeadingRange = (input) => {
7176
if (typeof input !== 'number' && (typeof input !== 'object' || input === null)) {
72-
throw new Error(defaultErrors.invalidHeadingLevels);
77+
showErrorMessage(options.debug, errorMessage.invalidHeadingLevels);
7378
}
7479

7580
const isInRange = (value, min, max) => value >= min && value <= max;
@@ -83,16 +88,16 @@
8388
({ start, finish } = input);
8489

8590
if (typeof start !== 'number' || typeof finish !== 'number') {
86-
throw new Error(defaultErrors.nonNumericValue);
91+
showErrorMessage(options.debug, errorMessage.nonNumericValue);
8792
}
8893

8994
if (start > finish) {
90-
throw new Error(defaultErrors.invalidHeadingLevelOrder);
95+
showErrorMessage(options.debug, errorMessage.invalidHeadingLevelOrder);
9196
}
9297
}
9398

9499
if (!isInRange(start, 1, 6) || !isInRange(finish, 1, 6)) {
95-
throw new Error(defaultErrors.headingLevelRange);
100+
showErrorMessage(options.debug, errorMessage.headingLevelRange);
96101
}
97102

98103
const headings = {};
@@ -112,7 +117,7 @@
112117
const isAlphabetic = elements.every(isAllAlphabetic);
113118

114119
if (!(isNumeric || isAlphabetic)) {
115-
throw new Error(defaultErrors.invalidHeadingLevels);
120+
showErrorMessage(options.debug, errorMessage.invalidHeadingLevels);
116121
}
117122

118123
while (elements.length < 6) {
@@ -130,10 +135,12 @@
130135
const checkAutoHeader = (markdown) => {
131136
const autoHeaderPattern = /^(?:@autoHeader:|<!-- autoHeader:)([\d.a-zA-Z\-:,~]+)(?: -->)?/;
132137
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];
134142
};
135143

136-
137144
const createCountContextObjects = (levels) => {
138145
const configEntries = Object.entries(levels);
139146
const currentCounts = new Map(
@@ -152,7 +159,7 @@
152159
};
153160
};
154161

155-
const applyCurrentCountThroughBoundContext = function (headingNode, options) {
162+
const applyCurrentCountThroughBoundContext = (headingNode, options) => {
156163
const { currentCounts, scopedTagNames } = this;
157164
const headingName = headingNode.tagName.toLowerCase();
158165
const headingNameList = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
@@ -241,26 +248,30 @@
241248
const defaultOptions = setDefaultOptions(docsifyAutoHeadersDefaults);
242249
let options = {
243250
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
247255
};
248256

249257
// MARK: Need to access markdown to extract the signifier data
250258
hook.beforeEach((markdown, next) => {
259+
let cleanedMarkdown;
251260
try {
252261
const headingSignifier = checkAutoHeader(markdown);
253262
if (!headingSignifier) {
254-
throw new Error(defaultErrors.missingAutoHeaderSignifier);
263+
options.shouldContinue = false;
264+
showErrorMessage(options.debug, errorMessage.missingAutoHeaderSignifier);
255265
}
256266

257-
const headingRanges = getHeadingRange(defaultOptions.levels);
267+
const headingRanges = validateAndGetHeadingRange(defaultOptions.levels);
258268
const startingHeadingValues = getStartingValues(
259269
headingSignifier,
260270
options.separator
261271
);
262272
if (!startingHeadingValues) {
263-
throw new Error(defaultErrors.misconfiguredSignifier);
273+
options.shouldContinue = false;
274+
showErrorMessage(options.debug, errorMessage.misconfiguredSignifier);
264275
}
265276
const headingConfiguration = {};
266277
for (const key in headingRanges) {
@@ -272,22 +283,24 @@
272283

273284
// Update the options for use elsewhere
274285
options.levels = headingConfiguration;
286+
options.shouldContinue = true;
275287

276-
const cleanedMarkdown = markdown.split('\n').slice(1).join('\n');
277-
return cleanedMarkdown;
278-
288+
cleanedMarkdown = markdown.split('\n').slice(1).join('\n');
279289
} catch (error) {
290+
options.shouldContinue = false;
280291
console.warn('Warning: Docsify Auto Headers\n', error.message);
281292
} finally {
282-
next(markdown);
293+
next(cleanedMarkdown);
283294
}
284295
});
285296

286297
// Conditional setup for hooks
287298
if (options.sidebar) {
288-
289-
290299
hook.beforeEach((markdown, next) => {
300+
if (!options.shouldContinue) {
301+
showErrorMessage(options.debug, errorMessage.exitingError);
302+
}
303+
291304
let output;
292305
try {
293306
output = applyScopedHeadingCounts(
@@ -297,13 +310,18 @@
297310
'markdown'
298311
);
299312
} catch (error) {
300-
console.warn('Warning: Docsify Auto Headers\n', error.message)
313+
console.warn('Warning: Docsify Auto Headers - beforeEach 2\n', error.message);
301314
} finally {
302315
next(output);
303316
}
304317
});
318+
305319
} else {
306320
hook.afterEach(function (html, next) {
321+
if (!options.shouldContinue) {
322+
showErrorMessage(options.debug, errorMessage.exitingError);
323+
}
324+
307325
let output;
308326
try {
309327
output = applyScopedHeadingCounts(
@@ -313,7 +331,7 @@
313331
'html'
314332
);
315333
} catch (error) {
316-
console.warn('Warning: Docsify Auto Headers\n', error.message)
334+
console.warn('Warning: Docsify Auto Headers\n', error.message);
317335
} finally {
318336
next(output);
319337
}

0 commit comments

Comments
 (0)