Skip to content

Commit

Permalink
Format and code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
chalin committed Aug 14, 2023
1 parent 8140780 commit c0caef5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
26 changes: 15 additions & 11 deletions gulp-src/_md-rules/trim-code-block-and-unindent.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,31 @@ const { trimBlankLinesFromArray } = require('../_util');

module.exports = {
names: ['trim-code-block-and-unindent'],
description: 'Code block avoid leading/trailing empty lines and unnecessary indentation',
description:
'Code block: avoid leading/trailing empty lines and unnecessary indentation',
tags: ['custom'],
function: (params, onError) => {
// const frontMatterLines = params.frontMatterLines;

params.tokens.forEach(token => {
if (token.type === 'fence') { // Checking if it's a code block excluding plain text
params.tokens.forEach((token) => {
if (token.type === 'fence') {
const lines = token.content.split('\n');
const originalNumLines = lines.length;

let minIndent = Infinity;
for (const line of lines) {
if (line.trim().length > 0) { // Skip blank-only lines
if (line.trim().length > 0) {
const currentIndent = line.length - line.trimStart().length;
minIndent = Math.min(minIndent, currentIndent);
}
}

let fixedLines = lines.map(line => {
if (line.trim().length === 0) return ''; // Blank-only lines
return line.substring(minIndent); // Unindent
});
let fixedLines = lines.map(
(line) =>
line.trim().length === 0
? '' // Blank-only lines
: line.substring(minIndent), // Unindent
);
fixedLines = trimBlankLinesFromArray(fixedLines);
const fixedContent = fixedLines.join('\n');

Expand All @@ -38,15 +41,16 @@ module.exports = {
context: token.line,
fixInfo: {
// Using the following as an endLineNumber
lineNumber: token.map[0] + originalNumLines + offset - codeFenceLineCount,
lineNumber:
token.map[0] + originalNumLines + offset - codeFenceLineCount,
insertText: fixedContent,
}
},
};
// console.log(JSON.stringify(args, null, 2));
// console.log(`${token.lineNumber} - Line ${token.map[0]} to ${token.map[1]}:`, token.content);
onError(args);
}
}
});
}
},
};
2 changes: 1 addition & 1 deletion gulp-src/_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ exports.trimBlankLinesFromArray = (lines) => {
}

return lines;
}
};
9 changes: 5 additions & 4 deletions gulp-src/lint-md.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function markdownLintFile(file, encoding, callback) {
}
numFilesProcessed++;

if(fix) {
if (fix) {
applyCustomRuleFixesHack(result);
}

Expand All @@ -81,7 +81,7 @@ function applyCustomRuleFixesHack(result) {
// Sort issues by lineNumber in descending order
const sortedIssues = issues.sort((a, b) => b.lineNumber - a.lineNumber);

sortedIssues.forEach(issue => {
sortedIssues.forEach((issue) => {
if (issue.fixInfo) {
fileContent = applyFixesToFileContent(fileContent, issue);
}
Expand All @@ -95,8 +95,9 @@ function applyFixesToFileContent(content, issue) {
// console.log(JSON.stringify(issue, null, 2));

const startLineNum = issue.lineNumber - 1;
const endLineNum = issue.ruleNames.includes("trim-code-block-and-unindent") ?
issue.fixInfo.lineNumber : startLineNum + 1;
const endLineNum = issue.ruleNames.includes('trim-code-block-and-unindent')
? issue.fixInfo.lineNumber
: startLineNum + 1;
const fixedLines = issue.fixInfo.insertText.split('\n');

// Remove lines that need fixing
Expand Down

0 comments on commit c0caef5

Please sign in to comment.