Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "textlint-rule-allowed-uris",
"version": "1.1.1",
"type": "module",
"packageManager": "[email protected]",
"workspaces": [
"."
Expand Down
16 changes: 8 additions & 8 deletions src/textlint-rule-allowed-uris.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
*/

// --------------------------------------------------------------------------------
// Require
// Import
// --------------------------------------------------------------------------------

const { error, strikethrough } = require('./utils/theme');
const {
import { error, strikethrough } from './utils/theme/index.js';
import {
getUriTypesLink,
getUriTypesImage,
getUriTypesDefinition,
getUriTypesHtml,
} = require('./utils/get-uri-types');
} from './utils/get-uri-types/index.js';

// --------------------------------------------------------------------------------
// Typedefs
Expand All @@ -22,19 +22,19 @@ const {
/**
* @import { TextlintRuleContext } from '@textlint/types';
* @import { TxtLinkNode, TxtImageNode, TxtDefinitionNode, TxtHtmlNode } from '@textlint/ast-node-types';
* @import { UriType, Options } from './types';
* @import { UriType, Options } from './types/index.js';
*/

// --------------------------------------------------------------------------------
// Exports
// Export
// --------------------------------------------------------------------------------

/**
* Entry point for the `textlint-rule-allowed-uris` rule.
* @param {TextlintRuleContext} context Context object.
* @param {Options} rawOptions Configuration options.
*/
module.exports = (context, rawOptions) => {
export default function textlintRuleAllowedUris(context, rawOptions) {
/** @type {Options} */
const options = {
allowed: {
Expand Down Expand Up @@ -99,4 +99,4 @@ module.exports = (context, rawOptions) => {
return report(node, getUriTypesHtml(node).uriTypes);
},
};
};
}
4 changes: 2 additions & 2 deletions src/types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/

// --------------------------------------------------------------------------------
// Exports
// Export
// --------------------------------------------------------------------------------

module.exports = {};
export default {};
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
*/

// --------------------------------------------------------------------------------
// Require
// Import
// --------------------------------------------------------------------------------

const url = require('node:url');
const mime = require('mime-types');
const { error } = require('../theme');
import url from 'node:url';
import mime from 'mime-types';
import { error } from '../theme/index.js';

// --------------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -48,8 +48,8 @@ const getMimeType = async uri => {
* @returns {Promise<'comment' | 'image' | 'link'>} Resolves to `'comment'` for empty(` `) or hash-only(`#`) URIs, `'image'` if the URI's MIME type is an image, and `'link'` for other types of URIs.
* @async
*/
module.exports = async uri => {
export default async function getDefinitionNodeUriType(uri) {
if (['', '#'].includes(uri)) return 'comment';
if ((await getMimeType(uri)).startsWith('image/')) return 'image';
return 'link';
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
*/

// --------------------------------------------------------------------------------
// Require
// Import
// --------------------------------------------------------------------------------

const { strictEqual } = require('node:assert');
const { describe, it } = require('node:test');
import { strictEqual } from 'node:assert';
import { describe, it } from 'node:test';

const getDefinitionNodeUriType = require('./get-definition-node-uri-type');
import getDefinitionNodeUriType from './get-definition-node-uri-type.js';

// --------------------------------------------------------------------------------
// Test
Expand Down
4 changes: 2 additions & 2 deletions src/utils/get-definition-node-uri-type/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const getDefinitionNodeUriType = require('./get-definition-node-uri-type');
import getDefinitionNodeUriType from './get-definition-node-uri-type.js';

module.exports = getDefinitionNodeUriType;
export default getDefinitionNodeUriType;
32 changes: 11 additions & 21 deletions src/utils/get-uri-types/get-uri-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
*/

// --------------------------------------------------------------------------------
// Require
// Import
// --------------------------------------------------------------------------------

const cheerio = require('cheerio');

const UriTypes = require('../uri-types');
const getDefinitionNodeUriType = require('../get-definition-node-uri-type');
import * as cheerio from 'cheerio';
import UriTypes from '../uri-types/index.js';
import getDefinitionNodeUriType from '../get-definition-node-uri-type/index.js';

// --------------------------------------------------------------------------------
// Typedefs
Expand All @@ -20,30 +19,32 @@ const getDefinitionNodeUriType = require('../get-definition-node-uri-type');
*/

// --------------------------------------------------------------------------------
// Helpers
// Export
// --------------------------------------------------------------------------------

/**
* Retrieves URI from a given `Link` node and returns an instance of `UriTypes`.
* @param {TxtLinkNode} node `Link` type node.
* @return {UriTypes}
*/
const getUriTypesLink = ({ url }) => new UriTypes().push({ uri: url, type: 'link' });
export const getUriTypesLink = ({ url }) =>
new UriTypes().push({ uri: url, type: 'link' });

/**
* Retrieves URI from a given `Image` node and returns an instance of `UriTypes`.
* @param {TxtImageNode} node `Image` type node.
* @return {UriTypes}
*/
const getUriTypesImage = ({ url }) => new UriTypes().push({ uri: url, type: 'image' });
export const getUriTypesImage = ({ url }) =>
new UriTypes().push({ uri: url, type: 'image' });

/**
* Retrieves URI from a given `Definition` node and returns an instance of `UriTypes`.
* @param {TxtDefinitionNode} node `Definition` type node.
* @return {Promise<UriTypes>}
* @async
*/
const getUriTypesDefinition = async ({ url }) => {
export const getUriTypesDefinition = async ({ url }) => {
const type = await getDefinitionNodeUriType(url);

return type === 'link' || type === 'image'
Expand All @@ -56,7 +57,7 @@ const getUriTypesDefinition = async ({ url }) => {
* @param {TxtHtmlNode} node `Html` type node.
* @return {UriTypes}
*/
const getUriTypesHtml = ({ value }) => {
export const getUriTypesHtml = ({ value }) => {
const uriTypes = new UriTypes();
const $ = cheerio.load(value);

Expand All @@ -78,14 +79,3 @@ const getUriTypesHtml = ({ value }) => {

return uriTypes;
};

// --------------------------------------------------------------------------------
// Exports
// --------------------------------------------------------------------------------

module.exports = {
getUriTypesLink,
getUriTypesImage,
getUriTypesDefinition,
getUriTypesHtml,
};
10 changes: 5 additions & 5 deletions src/utils/get-uri-types/get-uri-types.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
*/

// --------------------------------------------------------------------------------
// Require
// Import
// --------------------------------------------------------------------------------

const { deepStrictEqual } = require('node:assert');
const { describe, it } = require('node:test');
import { deepStrictEqual } from 'node:assert';
import { describe, it } from 'node:test';

const {
import {
getUriTypesLink,
getUriTypesImage,
getUriTypesDefinition,
getUriTypesHtml,
} = require('./get-uri-types');
} from './get-uri-types.js';

// --------------------------------------------------------------------------------
// Test
Expand Down
4 changes: 1 addition & 3 deletions src/utils/get-uri-types/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
const getUriTypes = require('./get-uri-types');

module.exports = getUriTypes;
export * from './get-uri-types.js';
4 changes: 1 addition & 3 deletions src/utils/theme/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
const theme = require('./theme');

module.exports = theme;
export * from './theme.js';
6 changes: 3 additions & 3 deletions src/utils/theme/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
*/

// --------------------------------------------------------------------------------
// Exports
// Export
// --------------------------------------------------------------------------------

/** Console error theme. @param {string} str */
module.exports.error = str => `\u001b[31m${str}\u001b[0m`;
export const error = str => `\u001b[31m${str}\u001b[0m`;
/** Console strikethrough theme. @param {string} str */
module.exports.strikethrough = str => `\u001b[9m${str}\u001b[0m`;
export const strikethrough = str => `\u001b[9m${str}\u001b[0m`;
4 changes: 2 additions & 2 deletions src/utils/uri-types/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const UriTypes = require('./uri-types');
import UriTypes from './uri-types.js';

module.exports = UriTypes;
export default UriTypes;
6 changes: 3 additions & 3 deletions src/utils/uri-types/uri-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// --------------------------------------------------------------------------------

/**
* @import { UriType } from '../../types';
* @import { UriType } from '../../types/index.js';
*/

// --------------------------------------------------------------------------------
Expand Down Expand Up @@ -92,7 +92,7 @@ class UriTypes {
}

// --------------------------------------------------------------------------------
// Exports
// Export
// --------------------------------------------------------------------------------

module.exports = UriTypes;
export default UriTypes;
8 changes: 4 additions & 4 deletions src/utils/uri-types/uri-types.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
*/

// --------------------------------------------------------------------------------
// Require
// Import
// --------------------------------------------------------------------------------

const { deepStrictEqual, throws } = require('node:assert');
const { describe, it } = require('node:test');
import { deepStrictEqual, throws } from 'node:assert';
import { describe, it } from 'node:test';

const UriTypes = require('./uri-types');
import UriTypes from './uri-types.js';

// --------------------------------------------------------------------------------
// Test
Expand Down
15 changes: 8 additions & 7 deletions tests/textlint-rule-allowed-uris.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@
*/

// --------------------------------------------------------------------------------
// Require
// Import
// --------------------------------------------------------------------------------

const { readFileSync } = require('node:fs');
const { resolve } = require('node:path');
const { test } = require('node:test');
import { createRequire } from 'node:module';
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { test } from 'node:test';

const TextLintTester = require('textlint-tester').default;
import rule from '../src/textlint-rule-allowed-uris.js';

const rule = require('../src/textlint-rule-allowed-uris');
const TextLintTester = createRequire(import.meta.url)('textlint-tester').default;

// --------------------------------------------------------------------------------
// Helpers
// --------------------------------------------------------------------------------

const tester = new TextLintTester();
const text = readFileSync(
resolve(__dirname, 'textlint-rule-allowed-uris.data.md'),
resolve(import.meta.dirname, 'textlint-rule-allowed-uris.data.md'),
'utf-8',
);

Expand Down
Loading