vitepress-linkcard 1.3.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,68 @@
1
1
  /**
2
- * @param url
3
- * @returns
2
+ * XHR module for fetching remote URL content.
3
+ *
4
+ * This module provides both synchronous and asynchronous HTTP GET request functionality
5
+ * with built-in caching. It works in both browser and Node.js environments by using
6
+ * the appropriate XMLHttpRequest implementation.
7
+ *
8
+ * @module xhr
9
+ * @todo Replace xmlhttprequest with a modern alternative (e.g., node-fetch or axios)
10
+ */
11
+ /**
12
+ * Performs a synchronous HTTP GET request to fetch HTML content from a URL.
13
+ *
14
+ * This function fetches the content synchronously, which means it blocks execution
15
+ * until the request completes. The response is cached to avoid redundant requests.
16
+ *
17
+ * @param url - The URL to fetch content from
18
+ * @returns The HTML content as a string, or undefined if the request fails
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * const html = sync('https://example.com')
23
+ * if (html) {
24
+ * console.log('Fetched HTML content')
25
+ * }
26
+ * ```
27
+ *
28
+ * @remarks
29
+ * - Returns cached content if available
30
+ * - Only returns content if HTTP status is 200
31
+ * - Errors are logged to console but not thrown
32
+ * - Synchronous requests block the event loop - use with caution
33
+ *
34
+ * @see {@link async} for an asynchronous alternative
4
35
  */
5
36
  export declare function sync(url: string): string | undefined;
6
37
  /**
7
- * @param url
8
- * @returns
38
+ * Performs an asynchronous HTTP GET request to fetch HTML content from a URL.
39
+ *
40
+ * This function fetches content asynchronously using Promises. The response is
41
+ * cached to avoid redundant requests.
42
+ *
43
+ * @param url - The URL to fetch content from
44
+ * @returns A Promise that resolves to the HTML content string, or undefined if the request fails
45
+ *
46
+ * @example
47
+ * ```typescript
48
+ * async function fetchPage() {
49
+ * try {
50
+ * const html = await async('https://example.com')
51
+ * if (html) {
52
+ * console.log('Fetched HTML content')
53
+ * }
54
+ * } catch (error) {
55
+ * console.error('Failed to fetch:', error)
56
+ * }
57
+ * }
58
+ * ```
59
+ *
60
+ * @remarks
61
+ * - Returns cached content if available
62
+ * - Only resolves with content if HTTP status is 200 and readyState is 4
63
+ * - Promise is rejected if the request throws an error
64
+ * - Preferred over `sync()` for non-blocking operations
65
+ *
66
+ * @see {@link sync} for a synchronous alternative
9
67
  */
10
68
  export declare function async(url: string): Promise<string | undefined>;
@@ -1,6 +1,31 @@
1
1
  import type { LinkToCardPlugin } from './types';
2
2
  /**
3
- * @param md
4
- * @param pluginOptions
3
+ * Markdown-it plugin that converts specially-formatted links into rich link preview cards.
4
+ *
5
+ * This plugin intercepts markdown links that start with the `@:` prefix and transforms them
6
+ * into interactive cards displaying metadata (title, description, logo) fetched from the target URL.
7
+ *
8
+ * ## Usage
9
+ *
10
+ * In your markdown file:
11
+ * ```md
12
+ * [@:https://example.com](Link Title)
13
+ * ```
14
+ *
15
+ * Plugin configuration:
16
+ * ```typescript
17
+ * import MarkdownIt from 'markdown-it'
18
+ * import { linkToCardPlugin } from 'vitepress-linkcard'
19
+ *
20
+ * const md = new MarkdownIt()
21
+ * md.use(linkToCardPlugin, {
22
+ * target: '_blank'
23
+ * })
24
+ * ```
25
+ *
26
+ * @param md - The markdown-it instance
27
+ * @param pluginOptions - Configuration options for the plugin
28
+ *
29
+ * @see {@link LinkToCardPluginOptions} for available options
5
30
  */
6
31
  export declare const linkToCardPlugin: LinkToCardPlugin;
package/types/types.d.ts CHANGED
@@ -1,25 +1,124 @@
1
1
  import type MarkdownIt from 'markdown-it';
2
+ /**
3
+ * Type definition for the link-to-card plugin compatible with markdown-it.
4
+ *
5
+ * @see {@link https://markdown-it.github.io/markdown-it/#MarkdownIt.use | MarkdownIt.use}
6
+ */
2
7
  export type LinkToCardPlugin = MarkdownIt.PluginWithOptions<LinkToCardPluginOptions>;
8
+ /**
9
+ * Configuration options for the link-to-card plugin.
10
+ *
11
+ * This interface defines the customization options available when using the vitepress-linkcard plugin.
12
+ * The plugin converts specially-formatted markdown links (prefixed with `@:`) into rich link preview cards
13
+ * with Open Graph Protocol (OGP) metadata.
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * import { linkToCardPlugin } from 'vitepress-linkcard'
18
+ *
19
+ * markdownIt.use(linkToCardPlugin, {
20
+ * target: '_blank'
21
+ * })
22
+ * ```
23
+ *
24
+ * @see {@link https://daringfireball.net/projects/markdown/syntax#link | Markdown Link Syntax}
25
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#target | HTML anchor target attribute}
26
+ */
3
27
  export interface LinkToCardPluginOptions {
28
+ /**
29
+ * Where to display the linked URL.
30
+ * @defaultValue `'_blank'` - Opens the link in a new tab/window
31
+ */
4
32
  target?: ATarget;
33
+ /**
34
+ * CSS class name prefix for the card DOM elements.
35
+ * When set, no inline styles are injected; only the class names are applied.
36
+ *
37
+ * @example `'my-docs__link-card'`
38
+ */
5
39
  classPrefix?: string;
40
+ /**
41
+ * Custom function to render the DOM fragment for the link card.
42
+ * When provided, this function is used instead of the default card renderer.
43
+ */
6
44
  render?: CardDomRender;
7
- borderColor?: string;
8
- bgColor?: string;
9
45
  }
46
+ /**
47
+ * Metadata extracted from a URL's HTML page using Open Graph Protocol tags and meta tags.
48
+ *
49
+ * This interface represents the structured data fetched from a web page to populate the link card display.
50
+ * The metadata is typically extracted from `<meta>` tags, OGP tags, and other HTML elements.
51
+ *
52
+ * @see {@link https://ogp.me/ | Open Graph Protocol}
53
+ */
10
54
  export interface UrlMetadata {
55
+ /**
56
+ * The title of the page, extracted from `<title>` or `<meta property="og:title">` tags.
57
+ */
11
58
  title?: string;
59
+ /**
60
+ * The description of the page, extracted from `<meta name="description">` or
61
+ * `<meta property="og:description">` tags.
62
+ */
12
63
  description?: string;
64
+ /**
65
+ * The logo or icon URL for the page, extracted from `<meta property="og:image">` or
66
+ * `<link rel="icon">` tags.
67
+ */
13
68
  logo?: string;
69
+ /**
70
+ * Additional metadata properties that may be extracted from the page.
71
+ */
14
72
  [key: string]: unknown;
15
73
  }
74
+ /**
75
+ * Valid values for the HTML anchor element's `target` attribute.
76
+ *
77
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#target | MDN - target attribute}
78
+ */
16
79
  export type ATarget = '_self' | '_blank' | '_top' | '_parent';
80
+ /**
81
+ * Options passed to the card DOM renderer function.
82
+ *
83
+ * This interface provides all the necessary information to render a link card,
84
+ * including the URL, display preferences, and styling options.
85
+ */
17
86
  export interface CardDomRenderOptions {
87
+ /**
88
+ * The URL to link to.
89
+ */
18
90
  href: string;
91
+ /**
92
+ * The title text to display on the link (typically from the markdown link text).
93
+ */
19
94
  linkTitle: string;
95
+ /**
96
+ * Where to display the linked URL when clicked.
97
+ */
20
98
  target: ATarget;
99
+ /**
100
+ * CSS class name prefix for the card DOM elements.
101
+ */
21
102
  classPrefix?: string;
22
- borderColor?: string;
23
- bgColor?: string;
24
103
  }
104
+ /**
105
+ * Function signature for custom card DOM renderers.
106
+ *
107
+ * This type defines a function that takes URL metadata and rendering options,
108
+ * and returns an HTML string representing the link card.
109
+ *
110
+ * @param data - The metadata extracted from the URL
111
+ * @param options - Rendering and styling options for the card
112
+ * @returns An HTML string representing the rendered link card
113
+ *
114
+ * @example
115
+ * ```typescript
116
+ * const customRender: CardDomRender = (data, options) => {
117
+ * return `<div class="${options.classPrefix}">
118
+ * <h3>${data.title}</h3>
119
+ * <p>${data.description}</p>
120
+ * </div>`
121
+ * }
122
+ * ```
123
+ */
25
124
  export type CardDomRender = (data: UrlMetadata, options: CardDomRenderOptions) => string;