rehype-infer-description-meta
Version:
rehype plugin to infer file metadata from the document
73 lines (72 loc) • 2.9 kB
TypeScript
/**
* Infer file metadata from the description of a document.
*
* The result is stored on `file.data.meta.description` (and
* `file.data.meta.descriptionHast`).
*
* ###### Notes
*
* The description is inferred through three strategies:
*
* 1. If `options.selector` is set and an element for that found, then the
* description is the text of that element
* 2. Otherwise, if a comment is found with the text of `options.comment`, then
* the description is the text up to that comment
* 3. Otherwise, the description is the text up to `options.truncateSize`
*
* @param {Readonly<Options> | null | undefined} [options]
* Configuration (optional).
* @returns
* Transform.
*/
export default function rehypeInferDescriptionMeta(options?: Readonly<Options> | null | undefined): (tree: Root, file: VFile) => undefined;
export type Element = import('hast').Element;
export type Root = import('hast').Root;
export type VFile = import('vfile').VFile;
/**
* Configuration.
*/
export type Options = {
/**
* String to look for in a comment (default: `'more'`); one of the strategies
* is to look for this comment, everything before it is the description.
*/
comment?: string | null | undefined;
/**
* CSS selector of nodes to ignore (default: `'h1, script, style, noscript,
* template'`); used when looking for an excerpt comment or truncating the
* document.
*/
ignoreSelector?: string | null | undefined;
/**
* Whether to expose `file.data.meta.descriptionHast` (default: `false`);
* this is not used by `rehype-meta`, but could be useful to other plugins;
* the value contains the rich HTML elements rather than the plain text
* content.
*/
inferDescriptionHast?: boolean | null | undefined;
/**
* CSS selector to body of content (optional); useful to exclude other
* things, such as the head, ads, styles, scripts, and other random stuff, by
* focussing all strategies in one element.
*/
mainSelector?: string | null | undefined;
/**
* How far to search for the excerpt comment before bailing (default:
* `2048`); the goal of explicit excerpts is that they are assumed to be
* somewhat reasonably placed; this option prevents searching giant documents
* for some comment that probably won’t be found at the end.
*/
maxExcerptSearchSize?: number | null | undefined;
/**
* CSS selector to the description (optional); one of the strategies is to
* look for a certain element, useful if the description is nicely encoded
* in one element.
*/
selector?: string | null | undefined;
/**
* Number of characters to truncate to (default: `140`); one of the
* strategies is to truncate the document to a certain number of characters.
*/
truncateSize?: number | null | undefined;
};