Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ function codeImport(options = {}) {
? parseInt(res.groups.from, 10)
: undefined;
const toLine = res.groups.to ? parseInt(res.groups.to, 10) : undefined;
const fileAbsPath = path.resolve(file.dirname, filePath);

if (!options.basePath && !file.dirname) {
throw new Error("Unable to parse base file path. Please configure options.basePath or modify your tooling to include path data, like mdxOptions.filepath.")
}

const fileAbsPath = path.resolve(options.basePath || file.dirname, filePath);

if (options.async) {
promises.push(
Expand Down
66 changes: 66 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
const codeImport = require('./');
const remark = require('remark');
const path = require('path');
const os = require('os');

const input = q => `
\`\`\`js file=./__fixtures__/say-#-hi.js${q}
\`\`\`
`;

const basePathTestPath = path.resolve(process.cwd(), 'gatsby');
const basePathTestInput = q => `
\`\`\`js file=../__fixtures__/say-#-hi.js${q}
\`\`\`
`;

const absolutePathTestInput = q => `
\`\`\`js file=${path.normalize(process.cwd())}/__fixtures__/say-#-hi.js${q}
\`\`\`
`;

test('Basic file import', () => {
expect(
remark()
Expand All @@ -27,6 +39,60 @@ test('Basic file import', () => {
`);
});

test('Absolute file import', () => {
expect(
remark()
.use(codeImport, {})
.processSync({
contents: absolutePathTestInput(''),
path: path.resolve('test.md'),
})
.toString()
).toBe(`\`\`\`js file=${path.normalize(process.cwd())}/__fixtures__/say-#-hi.js
console.log('Hello remark-code-import!');${os.EOL}console.log('This is another line...');${os.EOL}console.log('This is the last line');${os.EOL}console.log('Oops, here is another');
\`\`\`
`);
});

test('Basic file import with basePath', () => {
expect(
remark()
.use(codeImport, {
basePath: basePathTestPath,
})
.processSync({
contents: basePathTestInput(''),
path: path.resolve('test.md'),
})
.toString()
).toMatchInlineSnapshot(`
"\`\`\`js file=../__fixtures__/say-#-hi.js
console.log('Hello remark-code-import!');
console.log('This is another line...');
console.log('This is the last line');
console.log('Oops, here is another');
\`\`\`
"
`);
});

test('Absolute file import with basePath', () => {
expect(
remark()
.use(codeImport, {
basePath: basePathTestPath,
})
.processSync({
contents: absolutePathTestInput(''),
path: path.resolve('test.md'),
})
.toString()
).toBe(`\`\`\`js file=${path.normalize(process.cwd())}/__fixtures__/say-#-hi.js
console.log('Hello remark-code-import!');${os.EOL}console.log('This is another line...');${os.EOL}console.log('This is the last line');${os.EOL}console.log('Oops, here is another');
\`\`\`
`);
});

test('File import using line numbers', () => {
expect(
remark()
Expand Down