add and ran script indent-nested-md-code.js
This commit is contained in:
parent
0cd5dec0e3
commit
73c626d041
@ -15,7 +15,7 @@
|
|||||||
* - Repeat for multiple nested "inner blocks" within the same outer block
|
* - Repeat for multiple nested "inner blocks" within the same outer block
|
||||||
*
|
*
|
||||||
* Notes:
|
* Notes:
|
||||||
* - We only consider backtick fences (```). Tilde fences (~~~) are uncommon in this repo and not targeted
|
* - We only consider backtick fences (```). Tilde fences (~~~) are uncommon in repo, and excluded
|
||||||
* - We preserve existing content and whitespace beyond the added indentation for nested fences
|
* - We preserve existing content and whitespace beyond the added indentation for nested fences
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -59,7 +59,8 @@ function getEffectiveExt(filename) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Regex helpers
|
// Regex helpers
|
||||||
const fenceLineRe = /^(?<indent> {0,3})(?<ticks>`{3,})(?<rest>.*)$/; // up to 3 spaces + ``` + anything
|
// up to 3 spaces + ``` + anything
|
||||||
|
const fenceLineRe = /^(?<indent> {0,3})(?<ticks>`{3,})(?<rest>.*)$/;
|
||||||
|
|
||||||
function processFile(filePath) {
|
function processFile(filePath) {
|
||||||
const original = fs.readFileSync(filePath, 'utf8');
|
const original = fs.readFileSync(filePath, 'utf8');
|
||||||
@ -92,7 +93,8 @@ function processFile(filePath) {
|
|||||||
const indentLen = (m.groups.indent || '').length;
|
const indentLen = (m.groups.indent || '').length;
|
||||||
const ticksLen = m.groups.ticks.length;
|
const ticksLen = m.groups.ticks.length;
|
||||||
const restTrim = (m.groups.rest || '').trim();
|
const restTrim = (m.groups.rest || '').trim();
|
||||||
const isOuterCloser = indentLen <= outerIndent.length && ticksLen === outerTicksLen && restTrim === '';
|
const isOuterCloser = indentLen <= outerIndent.length && ticksLen === outerTicksLen &&
|
||||||
|
restTrim === '';
|
||||||
if (isOuterCloser) {
|
if (isOuterCloser) {
|
||||||
// End of outer block
|
// End of outer block
|
||||||
inOuter = false;
|
inOuter = false;
|
||||||
@ -102,7 +104,7 @@ function processFile(filePath) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, treat as nested inner fence start; indent until the matching inner fence (inclusive)
|
// Otherwise, treat as nested inner fence start; indent til matching inner fence (inclusive)
|
||||||
changed = true;
|
changed = true;
|
||||||
const innerTicksLen = ticksLen;
|
const innerTicksLen = ticksLen;
|
||||||
lines[i] = ' ' + lines[i];
|
lines[i] = ' ' + lines[i];
|
||||||
@ -113,7 +115,8 @@ function processFile(filePath) {
|
|||||||
const m2 = innerLine.match(fenceLineRe);
|
const m2 = innerLine.match(fenceLineRe);
|
||||||
lines[i] = ' ' + innerLine;
|
lines[i] = ' ' + innerLine;
|
||||||
i++;
|
i++;
|
||||||
if (m2 && m2.groups.ticks.length === innerTicksLen) break; // we've indented the closing inner fence; stop
|
// we've indented the closing inner fence; stop
|
||||||
|
if (m2 && m2.groups.ticks.length === innerTicksLen) break;
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user