Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 25, 2024
1 parent 7718ceb commit 23429f1
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 51 deletions.
66 changes: 65 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
export type {Options} from './lib/index.js'
import type {Properties} from 'hastscript'

export {default} from './lib/index.js'

/**
* Fields supported by `rehype-document`.
*/
interface DocumentFields {
/**
* Title of the document (optional, example: `'The New York City Subway Map
Expand All @@ -14,6 +17,67 @@ interface DocumentFields {
title?: string | null | undefined
}

/**
* Configuration.
*/
export interface Options {
/**
* URLs to stylesheets to use in `<link>`s (optional).
*/
css?: ReadonlyArray<string> | string | null | undefined
/**
* Direction of the document (optional).
*/
dir?: 'auto' | 'ltr' | 'rtl' | null | undefined
/**
* URLs to scripts to use as `src` on `<script>`s (optional).
*/
js?: ReadonlyArray<string> | string | null | undefined
/**
* Language of document (default: `'en'`); should be a
* [BCP 47](https://tools.ietf.org/html/bcp47) language tag.
*/
language?: string | null | undefined
/**
* Generate extra `<link>`s with these properties (optional); passed as
* `properties` to [`hastscript`](https://github.com/syntax-tree/hastscript)
* with `'link'`.
*/
link?:
| ReadonlyArray<Readonly<Properties>>
| Readonly<Properties>
| null
| undefined
/**
* Generate extra `<meta>`s with these properties (optional); passed as
* `properties` to [`hastscript`](https://github.com/syntax-tree/hastscript)
* with `'meta'`.
*/
meta?:
| ReadonlyArray<Readonly<Properties>>
| Readonly<Properties>
| null
| undefined
/**
* Generate a `meta[viewport]` (default: `true`).
*/
responsive?: boolean | null | undefined
/**
* JavaScript source code of `<script>`s to add at end of `body` (optional).
*/
script?: ReadonlyArray<string> | string | null | undefined
/**
* CSS source code of `<style>`s to add (optional).
*/
style?: ReadonlyArray<string> | string | null | undefined
/**
* Text to use as title (optional); defaults to the file name (if any); can
* bet set with `file.data.matter.title` (`vfile-matter`) and
* `file.data.meta.title` (`rehype-infer-title-meta`), which are preferred.
*/
title?: string | null | undefined
}

// Add custom data supported when `rehype-document` is added.
declare module 'vfile' {
interface DataMapMatter extends DocumentFields {}
Expand Down
48 changes: 6 additions & 42 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,12 @@
/**
* @typedef {import('hast').ElementContent} ElementContent
* @typedef {import('hast').Nodes} Nodes
* @typedef {import('hast').Root} Root
*
* @typedef {import('hastscript').Properties} Properties
*
* @typedef {import('vfile').VFile} VFile
*/

/**
* @typedef Options
* Configuration.
* @property {Array<string> | string | null | undefined} [css]
* URLs to stylesheets to use in `<link>`s (optional).
* @property {'auto' | 'ltr' | 'rtl' | null | undefined} [dir]
* Direction of the document (optional).
* @property {Array<string> | string | null | undefined} [js]
* URLs to scripts to use as `src` on `<script>`s (optional).
* @property {string | null | undefined} [language='en']
* Language of document (default: `'en'`); should be a
* [BCP 47](https://tools.ietf.org/html/bcp47) language tag.
* @property {Array<Properties> | Properties | null | undefined} [link]
* Generate extra `<link>`s with these properties (optional); passed as
* `properties` to [`hastscript`](https://github.com/syntax-tree/hastscript)
* with `'link'`.
* @property {Array<Properties> | Properties | null | undefined} [meta]
* Generate extra `<meta>`s with these properties (optional); passed as
* `properties` to [`hastscript`](https://github.com/syntax-tree/hastscript)
* with `'meta'`.
* @property {boolean | null | undefined} [responsive=true]
* Generate a `meta[viewport]` (default: `true`).
* @property {Array<string> | string | null | undefined} [script]
* JavaScript source code of `<script>`s to add at end of `body` (optional).
* @property {Array<string> | string | null | undefined} [style]
* CSS source code of `<style>`s to add (optional).
* @property {string | null | undefined} [title]
* Text to use as title (optional); defaults to the file name (if any); can
* bet set with `file.data.matter.title` (`vfile-matter`) and
* `file.data.meta.title` (`rehype-infer-title-meta`), which are preferred.
* @import {ElementContent, Nodes, Root} from 'hast'
* @import {Options} from 'rehype-document'
* @import {VFile} from 'vfile'
*/

import {h} from 'hastscript'

/** @type {Options} */
/** @type {Readonly<Options>} */
const emptyOptions = {}

/**
Expand Down Expand Up @@ -165,7 +129,7 @@ export default function rehypeDocument(options) {
*
* @template Thing
* Value kind.
* @param {Array<Thing> | Thing | null | undefined} value
* @param {ReadonlyArray<Thing> | Thing | null | undefined} value
* Value to cast.
* @returns {Array<Thing>}
* List.
Expand All @@ -174,6 +138,6 @@ function toList(value) {
return value === null || value === undefined
? []
: Array.isArray(value)
? value
? [...value]
: [value]
}
20 changes: 17 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,25 @@
"overrides": [
{
"files": [
"*.ts"
"**/*.d.ts"
],
"rules": {
"@typescript-eslint/ban-types": "off",
"@typescript-eslint/consistent-type-definitions": "off"
"@typescript-eslint/array-type": [
"error",
{
"default": "generic"
}
],
"@typescript-eslint/ban-types": [
"error",
{
"extendDefaults": true
}
],
"@typescript-eslint/consistent-type-definitions": [
"error",
"interface"
]
}
}
],
Expand Down
9 changes: 4 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* @typedef {import('hast').Element} Element
* @import {Element} from 'hast'
* @import {Parser, Processor} from 'unified'
*/

import assert from 'node:assert/strict'
Expand Down Expand Up @@ -556,11 +557,9 @@ test('rehypeDocument()', async function (t) {
.use(function () {
// @ts-expect-error: TypeScript is bad at `this`.
// eslint-disable-next-line unicorn/no-this-assignment
const self = /** @type {import('unified').Processor<Element>} */ (
this
)
const self = /** @type {Processor<Element>} */ (this)

/** @type {import('unified').Parser<Element>} */
/** @type {Parser<Element>} */
self.parser = function () {
/** @type {Element} */
const node = {
Expand Down

0 comments on commit 23429f1

Please sign in to comment.