Skip to content

Commit

Permalink
refactor: code review feedback
Browse files Browse the repository at this point in the history
* include `void[]` as an options type
* create constant regex ahead of time
* document `defaultImageExtensions` in API section of readme
* add description to jsdoc/tsdoc options type

Co-authored-by: Titus Wormer <tituswormer@gmail.com>
  • Loading branch information
ChristianMurphy and wooorm committed Oct 31, 2021
1 parent dce6610 commit 4c9d59a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
16 changes: 8 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
* @typedef {import('mdast').Root} Root
* @typedef {import('mdast').Image} Image
* @typedef {import('mdast').Link} Link
*
* @typedef Options
* Configuration (optional).
* @property {Array<string>} [imageExtensions]
* File extensions (without dot) to treat as images.
*/

import isUrl from 'is-url'
import {visitParents} from 'unist-util-visit-parents'
import {is} from 'unist-util-is'

/**
* @typedef Options
* @property {Array<string>} [imageExtensions] file extensions of images
*/

const isImgPath = (/** @type {string} */ value) =>
value.startsWith('/') || value.startsWith('./') || value.startsWith('../')

Expand All @@ -32,13 +32,13 @@ export const defaultImageExtensions = [
/**
* Plugin to add an improved image syntax.
*
* @type {import('unified').Plugin<[Options?], Root>}
* @type {import('unified').Plugin<[Options?]|void[], Root>}
*/
export default function remarkImages({
imageExtensions = defaultImageExtensions
} = {}) {
const isImgExt = (/** @type {string} */ value) =>
new RegExp(`\\.(${imageExtensions.join('|')})$`).test(value)
const imgExtRegex = new RegExp(`\\.(${imageExtensions.join('|')})$`)
const isImgExt = (/** @type {string} */ value) => imgExtRegex.test(value)

return (tree) => {
visitParents(tree, 'text', (node, parents) => {
Expand Down
9 changes: 7 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,17 @@ Supported URLs / URIs:

##### `options`

Configuration.
Configuration (optional).

###### `options.imageExtensions`

List of file extensions recognized as images (`Array.<string>?`, default
`['svg', 'png', 'jpg', 'jpeg', 'gif', 'webp', 'avif']`).
[`defaultImageExtensions`](#defaultimageextensions)).

### `defaultImageExtensions`

list of file extensions recognized as an image by default (constant `['svg', 'png', 'jpg', 'jpeg', 'gif', 'webp', 'avif']`).
Note: extension does not include `.`, only the extension name.

## Security

Expand Down

0 comments on commit 4c9d59a

Please sign in to comment.