Skip to content
Merged
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
19 changes: 7 additions & 12 deletions src/textlint-rule-allowed-uris.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@
// Import
// --------------------------------------------------------------------------------

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

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

// --------------------------------------------------------------------------------
Expand Down Expand Up @@ -81,22 +76,22 @@ export default function textlintRuleAllowedUris(context, rawOptions) {
return {
/** @param {TxtLinkNode} node */
Link(node) {
return report(node, getUriTypesLink(node).uriTypes);
return report(node, [{ uri: node.url, type: 'link' }]);
},

/** @param {TxtImageNode} node */
Image(node) {
return report(node, getUriTypesImage(node).uriTypes);
return report(node, [{ uri: node.url, type: 'image' }]);
},

/** @param {TxtDefinitionNode} node */
async Definition(node) {
return report(node, (await getUriTypesDefinition(node)).uriTypes);
return report(node, await getUriTypesDefinition(node));
},

/** @param {TxtHtmlNode} node */
Html(node) {
return report(node, getUriTypesHtml(node).uriTypes);
return report(node, getUriTypesHtml(node));
},
};
}
29 changes: 0 additions & 29 deletions src/types/index.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import url from 'node:url';
import mime from 'mime-types';
import { error } from '../theme/index.js';
import { error } from '../theme.js';

// --------------------------------------------------------------------------------
// Helpers
Expand Down
33 changes: 8 additions & 25 deletions src/utils/get-uri-types/get-uri-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,41 @@
// --------------------------------------------------------------------------------

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

// --------------------------------------------------------------------------------
// Typedefs
// --------------------------------------------------------------------------------

/**
* @import { TxtLinkNode, TxtImageNode, TxtDefinitionNode, TxtHtmlNode } from '@textlint/ast-node-types';
* @import { TxtDefinitionNode, TxtHtmlNode } from '@textlint/ast-node-types';
* @import { UriType } from '../types.js';
*/

// --------------------------------------------------------------------------------
// Export
// --------------------------------------------------------------------------------

/**
* Retrieves URI from a given `Link` node and returns an instance of `UriTypes`.
* @param {TxtLinkNode} node `Link` type node.
* @return {UriTypes}
*/
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}
*/
export const getUriTypesImage = ({ url }) =>
new UriTypes().push({ uri: url, type: 'image' });

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

return type === 'link' || type === 'image'
? new UriTypes().push({ uri: url, type })
: new UriTypes();
return type === 'link' || type === 'image' ? [{ uri: url, type }] : [];
};

/**
* Parses the HTML content of the given node and retrieves all the `<a>` and `<img>` tag's URIs.
* @param {TxtHtmlNode} node `Html` type node.
* @return {UriTypes}
* @return {UriType[]}
*/
export const getUriTypesHtml = ({ value }) => {
const uriTypes = new UriTypes();
/** @type {UriType[]} */
const uriTypes = [];
const $ = cheerio.load(value);

$('a, img').each((_, elem) => {
Expand Down
Loading