Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converts package to ESM. #8

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
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
File renamed without changes.
File renamed without changes.
21 changes: 18 additions & 3 deletions __tests__/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ exports[`should handle mdx 1`] = `
import something from 'something';

export const meta = {
prop: 'value',
prop: 'value',
};

# Title
Expand All @@ -33,7 +33,7 @@ Paragraph 2.

`;

exports[`should handle no spacing in exceprt comment 1`] = `
exports[`should handle no spacing in excerpt comment 1`] = `
# Title

Paragraph 1.
Expand All @@ -49,7 +49,7 @@ Paragraph 1.

`;

exports[`should return excerpt using "exceprt" identifier 1`] = `
exports[`should return excerpt using "excerpt" identifier 1`] = `
# Title

Paragraph 1.
Expand Down Expand Up @@ -94,6 +94,21 @@ Paragraph 2.

`;

exports[`should return unmodified document when comment exists 1`] = `
# Title

Paragraph 1.

Paragraph 2.

<!-- comment -->

Paragraph 3.

Paragraph 4.

`;

exports[`should return unmodified document when no excerpt exists 1`] = `
# Title

Expand Down
11 changes: 11 additions & 0 deletions __tests__/fixtures/comment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Title

Paragraph 1.

Paragraph 2.

<!-- comment -->

Paragraph 3.

Paragraph 4.
26 changes: 18 additions & 8 deletions __tests__/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
const {join} = require('node:path');
import {dirname, join} from 'node:path';
import {fileURLToPath} from 'node:url';

const {wrap} = require('jest-snapshot-serializer-raw');
const remark = require('remark');
const vfile = require('to-vfile');
import {wrap} from 'jest-snapshot-serializer-raw';
import {remark} from 'remark';
import {toVFile} from 'to-vfile';

const plugin = require('..');
import plugin from '..';

const processFixture = async (name, options) => {
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const path = join(__dirname, 'fixtures', name);
const file = await vfile.read(path);
const file = await toVFile.read(path);

const result = await remark().use(plugin, options).process(file);

return result.toString();
};

test('should return unmodified document when comment exists', async () => {
const result = await processFixture('comment.md');

expect(wrap(result)).toMatchSnapshot();
});

test('should return unmodified document when no excerpt exists', async () => {
const result = await processFixture('no-excerpt.md');

expect(wrap(result)).toMatchSnapshot();
});

test('should return excerpt using "exceprt" identifier', async () => {
test('should return excerpt using "excerpt" identifier', async () => {
const result = await processFixture('excerpt.md');

expect(wrap(result)).toMatchSnapshot();
Expand Down Expand Up @@ -77,7 +87,7 @@ test('should handle identifier with spaces', async () => {
expect(wrap(result)).toMatchSnapshot();
});

test('should handle no spacing in exceprt comment', async () => {
test('should handle no spacing in excerpt comment', async () => {
const result = await processFixture('no-space-in-comment.md');

expect(wrap(result)).toMatchSnapshot();
Expand Down
16 changes: 7 additions & 9 deletions example/example.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
const remark = require('remark');
const vfile = require('to-vfile');
import {remark} from 'remark';
import {toVFile} from 'to-vfile';

const plugin = require('..');
import plugin from '../index.js';

(async () => {
const file = await vfile.read('./example/example.md');
const result = await remark().use(plugin).process(file);
const file = await toVFile.read('./example/example.md');
const result = await remark().use(plugin).process(file);

// eslint-disable-next-line no-console
console.log(result.toString());
})();
// eslint-disable-next-line no-console
console.log(result.toString());
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const visit = require('unist-util-visit');
import {visit} from 'unist-util-visit';

const isComment = /<!--(.*?)-->/u;
const getComment = /<!--([\s\S]*?)-->/u;
Expand Down Expand Up @@ -34,4 +34,4 @@ const plugin = (options = {}) => {
return transformer;
};

module.exports = plugin;
export default plugin;
5 changes: 3 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module.exports = {
export default () => ({
coverageDirectory: '.coverage',
resetMocks: true,
resetModules: true,
restoreMocks: true,
snapshotSerializers: ['jest-snapshot-serializer-raw'],
testEnvironment: 'node',
};
transform: {},
});
Loading