Skip to content

Commit 121287a

Browse files
tschaubazu
authored andcommitted
fix: Make unindent work with sliced code (#54)
1 parent 5278db9 commit 121287a

File tree

6 files changed

+20
-3
lines changed

6 files changed

+20
-3
lines changed

src/parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ export function embedCode(kvMap, { filePath, originalPath, label }) {
210210
// Slice content via line numbers.
211211
if (hasSliceRange(label)) {
212212
const [start, end] = getSliceRange(label);
213-
content = sliceCode(code, start, end);
213+
content = sliceCode(code, start, end, unindent);
214214
} else if (hasMarker(kvm)) {
215215
// Slice content via markers.
216216
const marker = getMarker(kvm);

src/slicer.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ export function hasSliceRange(label) {
4646
* @param {string} code
4747
* @param {number|undefined} [start]
4848
* @param {number|undefined} [end]
49+
* @param {boolean|undefined} [untrimmed]
4950
* @returns {string}
5051
*/
51-
export function sliceCode(code, start, end) {
52+
export function sliceCode(code, start, end, untrimmed) {
5253
if (start === undefined && end === undefined) {
5354
return code;
5455
}
@@ -59,5 +60,6 @@ export function sliceCode(code, start, end) {
5960
if (end === undefined) {
6061
end = slitted.length;
6162
}
62-
return slitted.slice(start - 1, end).join("\n").trim();
63+
const sliced = slitted.slice(start - 1, end).join("\n");
64+
return untrimmed ? sliced : sliced.trim();
6365
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[import:1-2](test.js)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
pluginsConfig: {
3+
"include-codeblock": {
4+
unindent: true
5+
}
6+
}
7+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
``` javascript
2+
foo;
3+
bar;
4+
```
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
foo;
2+
bar;
3+
baz;

0 commit comments

Comments
 (0)