From a6c56e536a380b2f64025ef09b170afddb95459b Mon Sep 17 00:00:00 2001 From: Niccolo Borgioli Date: Thu, 16 Nov 2023 15:43:48 +0100 Subject: [PATCH] trim content by default --- package.json | 2 +- src/index.ts | 4 ++++ tests/__snapshots__/base.test.ts.snap | 2 -- tests/__snapshots__/options.test.ts.snap | 10 ++++++++++ tests/__snapshots__/recursion.test.ts.snap | 1 - tests/options.test.ts | 4 ++++ 6 files changed, 19 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index eb1485c..745c2a7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@nicco.io/markdown-it-import", - "version": "1.0.0", + "version": "1.0.1", "description": "Markdown-it plugin which adds the ability to include markdown fragment files.", "keywords": [ "markdown-it-plugin", diff --git a/src/index.ts b/src/index.ts index 545c615..3b47dba 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,6 +6,7 @@ function defaultOptions() { return { matcher: /@import\((?.+)\)(\s*?\[(?\d+-\d+)\])?/g, root: process.cwd(), + trim: true, } } @@ -40,6 +41,9 @@ export function importPlugin(md: any, options: Options = {}) { const exists = fs.existsSync(filename) if (!exists) throw new Error(`cannot locate file "${filename}"`) let contents = fs.readFileSync(filename, 'utf-8') + if (o.trim) { + contents = contents.trim() + } // Apply line range if (range) { diff --git a/tests/__snapshots__/base.test.ts.snap b/tests/__snapshots__/base.test.ts.snap index 9f6416c..da866d9 100644 --- a/tests/__snapshots__/base.test.ts.snap +++ b/tests/__snapshots__/base.test.ts.snap @@ -34,7 +34,6 @@ exports[`base single import 1`] = `
export function sum(a: number, b: number): number {
   return a + b
 }
-
 
" `; @@ -44,7 +43,6 @@ exports[`base multiple imports 1`] = `
export function sum(a: number, b: number): number {
   return a + b
 }
-
 

This is some amazing quote

diff --git a/tests/__snapshots__/options.test.ts.snap b/tests/__snapshots__/options.test.ts.snap index 43a1400..9bad506 100644 --- a/tests/__snapshots__/options.test.ts.snap +++ b/tests/__snapshots__/options.test.ts.snap @@ -38,3 +38,13 @@ exports[`options empty options 1`] = ` " `; + +exports[`options do not trim 1`] = ` +"

Here is a typescript snippet

+
export function sum(a: number, b: number): number {
+  return a + b
+}
+
+
+" +`; diff --git a/tests/__snapshots__/recursion.test.ts.snap b/tests/__snapshots__/recursion.test.ts.snap index ce56d3b..17ac4b4 100644 --- a/tests/__snapshots__/recursion.test.ts.snap +++ b/tests/__snapshots__/recursion.test.ts.snap @@ -9,7 +9,6 @@ exports[`base import different files 1`] = `
export function sum(a: number, b: number): number {
   return a + b
 }
-
 
" `; diff --git a/tests/options.test.ts b/tests/options.test.ts index 499bf0c..eef4608 100644 --- a/tests/options.test.ts +++ b/tests/options.test.ts @@ -30,4 +30,8 @@ describe('options', () => { expect(() => render('matcher-a.md', { matcher })).toThrow('Regexp must expose a named group "file"') }) }) + + test('do not trim', async () => { + expect(await render('whole.md', { trim: false })).toMatchSnapshot() + }) })