Skip to content

Commit

Permalink
Add JSDoc based types
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 3, 2021
1 parent 500e6a7 commit d199ec2
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 47 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
coverage/
node_modules/
.DS_Store
*.d.ts
*.log
yarn.lock
98 changes: 53 additions & 45 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,65 @@
/**
* @typedef {import('mdast').Root} Root
* @typedef {import('mdast').Image} Image
* @typedef {import('mdast').Link} Link
*/

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

const isImgExt = (value) => /\.(svg|png|jpg|jpeg|gif)$/.test(value)
const isAbsolutePath = (value) => value.startsWith('/')
const isRelativePath = (value) =>
value.startsWith('./') || value.startsWith('../')
const isImgPath = (value) => isAbsolutePath(value) || isRelativePath(value)
const isInteractive = convert(['link', 'linkReference'])
const isImgExt = (/** @type {string} */ value) =>
/\.(svg|png|jpg|jpeg|gif)$/.test(value)
const isImgPath = (/** @type {string} */ value) =>
value.startsWith('/') || value.startsWith('./') || value.startsWith('../')

/**
* Plugin to add an improved image syntax.
*
* @type {import('unified').Plugin<void[], Root>}
*/
export default function remarkImages() {
return transform
}
return (tree) => {
visitParents(tree, 'text', (node, parents) => {
const value = String(node.value).trim()

function transform(tree) {
visitParents(tree, 'text', ontext)
}
if ((isUrl(value) || isImgPath(value)) && isImgExt(value)) {
let interactive = false
let length = parents.length
const siblings = parents[length - 1].children

function ontext(node, parents) {
const value = String(node.value).trim()
// Check if we’re in interactive content.
while (length--) {
if (is(parents[length], ['link', 'linkReference'])) {
interactive = true
break
}
}

if ((isUrl(value) || isImgPath(value)) && isImgExt(value)) {
let interactive = false
let length = parents.length
const siblings = parents[length - 1].children
/** @type {Image} */
const image = {
type: 'image',
url: value,
title: null,
alt: '',
position: node.position
}
/** @type {Image|Link} */
let next = image

// Check if we’re in interactive content.
while (length--) {
if (isInteractive(parents[length])) {
interactive = true
break
}
}

let next = {
type: 'image',
url: value,
title: null,
alt: null,
position: node.position
}

// Add a link if we’re not already in one.
if (!interactive) {
next = {
type: 'link',
url: value,
title: null,
children: [next],
position: node.position
}
}
// Add a link if we’re not already in one.
if (!interactive) {
next = {
type: 'link',
url: value,
title: null,
children: [image],
position: node.position
}
}

siblings[siblings.indexOf(node)] = next
siblings[siblings.indexOf(node)] = next
}
})
}
}
20 changes: 18 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,38 @@
"sideEffects": false,
"type": "module",
"main": "index.js",
"types": "index.d.ts",
"files": [
"index.d.ts",
"index.js"
],
"dependencies": {
"is-url": "^1.2.2",
"@types/mdast": "^3.0.0",
"is-url": "^1.0.0",
"unified": "^10.0.0",
"unist-util-is": "^5.0.0",
"unist-util-visit-parents": "^5.0.0"
},
"devDependencies": {
"@types/is-url": "^1.0.0",
"@types/tape": "^4.0.0",
"c8": "^7.0.0",
"prettier": "^2.0.0",
"remark": "^14.0.0",
"remark-cli": "^10.0.0",
"remark-preset-wooorm": "^8.0.0",
"rimraf": "^3.0.0",
"tape": "^5.0.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"xo": "^0.39.0"
},
"scripts": {
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node --conditions development test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov npm run test-api",
"test": "npm run format && npm run test-coverage"
"test": "npm run build && npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
Expand All @@ -72,5 +82,11 @@
"plugins": [
"preset-wooorm"
]
},
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true,
"ignoreCatch": true
}
}
16 changes: 16 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"include": ["*.js"],
"compilerOptions": {
"target": "ES2020",
"lib": ["ES2020"],
"module": "ES2020",
"moduleResolution": "node",
"allowJs": true,
"checkJs": true,
"declaration": true,
"emitDeclarationOnly": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true,
"strict": true
}
}

0 comments on commit d199ec2

Please sign in to comment.