-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Add types for shortcode package #67416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gziolo
merged 4 commits into
WordPress:trunk
from
manzoorwanijk:update/add-types-for-shortcode-package
Nov 30, 2024
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,210 @@ | ||
| /** | ||
| * Shortcode attributes object. | ||
| */ | ||
| export type ShortcodeAttrs = { | ||
| /** | ||
| * Object with named attributes. | ||
| */ | ||
| named: Record< string, string | undefined >; | ||
|
|
||
| /** | ||
| * Array with numeric attributes. | ||
| */ | ||
| numeric: string[]; | ||
| }; | ||
|
|
||
| export type ShortcodeMatch = { | ||
| /** | ||
| * Index the shortcode is found at. | ||
| */ | ||
| index: number; | ||
|
|
||
| /** | ||
| * Matched content. | ||
| */ | ||
| content: string; | ||
|
|
||
| /** | ||
| * Shortcode instance of the match. | ||
| */ | ||
| shortcode: Shortcode; | ||
| }; | ||
|
|
||
| /** | ||
| * Shortcode options. | ||
| */ | ||
| export interface ShortcodeOptions { | ||
| /** | ||
| * Shortcode tag. | ||
| */ | ||
| tag: string; | ||
|
|
||
| /** | ||
| * Shortcode attributes. | ||
| */ | ||
| attrs?: Partial< ShortcodeAttrs > | string; | ||
|
|
||
| /** | ||
| * Shortcode content. | ||
| */ | ||
| content?: string; | ||
|
|
||
| /** | ||
| * Shortcode type: `self-closing`, `closed`, or `single`. | ||
| */ | ||
| type?: 'self-closing' | 'closed' | 'single'; | ||
| } | ||
|
|
||
| /** | ||
| * Shortcode object. | ||
| */ | ||
| export interface Shortcode extends ShortcodeOptions { | ||
| /** | ||
| * Shortcode attributes. | ||
| */ | ||
| attrs: ShortcodeAttrs; | ||
| } | ||
|
|
||
| export type Match = | ||
| | NonNullable< ReturnType< RegExp[ 'exec' ] > > | ||
| | Array< string >; | ||
|
|
||
| export type ReplaceCallback = ( shortcode: Shortcode ) => string; | ||
|
|
||
| /** | ||
| * WordPress Shortcode instance. | ||
| */ | ||
| export interface WPShortcode { | ||
| new ( options: Partial< ShortcodeOptions > ): Shortcode & { | ||
| /** | ||
| * Transform the shortcode into a string. | ||
| * | ||
| * @return {string} String representation of the shortcode. | ||
| */ | ||
| string: () => string; | ||
|
|
||
| /** | ||
| * Get a shortcode attribute. | ||
| * | ||
| * Automatically detects whether `attr` is named or numeric and routes it | ||
| * accordingly. | ||
| * | ||
| * @param {(number|string)} attr Attribute key. | ||
| * | ||
| * @return {string} Attribute value. | ||
| */ | ||
| get: ( attr: string | number ) => string | undefined; | ||
|
|
||
| /** | ||
| * Set a shortcode attribute. | ||
| * | ||
| * Automatically detects whether `attr` is named or numeric and routes it | ||
| * accordingly. | ||
| * | ||
| * @param {(number|string)} attr Attribute key. | ||
| * @param {string} value Attribute value. | ||
| * | ||
| * @return {InstanceType< WPShortcode >} Shortcode instance. | ||
| */ | ||
| set: ( | ||
| attr: string | number, | ||
| value: string | ||
| ) => InstanceType< WPShortcode >; | ||
| }; | ||
|
|
||
| /** | ||
| * Parse shortcode attributes. | ||
| * | ||
| * Shortcodes accept many types of attributes. These can chiefly be divided into | ||
| * named and numeric attributes: | ||
| * | ||
| * Named attributes are assigned on a key/value basis, while numeric attributes | ||
| * are treated as an array. | ||
| * | ||
| * Named attributes can be formatted as either `name="value"`, `name='value'`, | ||
| * or `name=value`. Numeric attributes can be formatted as `"value"` or just | ||
| * `value`. | ||
| * | ||
| * @param text Serialised shortcode attributes. | ||
| * | ||
| * @return Parsed shortcode attributes. | ||
| */ | ||
| attrs: ( text: string ) => ShortcodeAttrs; | ||
|
|
||
| /** | ||
| * Generate a Shortcode Object from a RegExp match. | ||
| * | ||
| * Accepts a `match` object from calling `regexp.exec()` on a `RegExp` generated | ||
| * by `regexp()`. `match` can also be set to the `arguments` from a callback | ||
| * passed to `regexp.replace()`. | ||
| * | ||
| * @param match Match array. | ||
| * | ||
| * @return Shortcode instance. | ||
| */ | ||
| fromMatch: ( match: Match ) => InstanceType< WPShortcode >; | ||
|
|
||
| /** | ||
| * Find the next matching shortcode. | ||
| * | ||
| * @param tag Shortcode tag. | ||
| * @param text Text to search. | ||
| * @param index Index to start search from. | ||
| * | ||
| * @return Matched information. | ||
| */ | ||
| next: ( | ||
| tag: string, | ||
| text: string, | ||
| index?: number | ||
| ) => ShortcodeMatch | undefined; | ||
|
|
||
| /** | ||
| * Generate a RegExp to identify a shortcode. | ||
| * | ||
| * The base regex is functionally equivalent to the one found in | ||
| * `get_shortcode_regex()` in `wp-includes/shortcodes.php`. | ||
| * | ||
| * Capture groups: | ||
| * | ||
| * 1. An extra `[` to allow for escaping shortcodes with double `[[]]` | ||
| * 2. The shortcode name | ||
| * 3. The shortcode argument list | ||
| * 4. The self closing `/` | ||
| * 5. The content of a shortcode when it wraps some content. | ||
| * 6. The closing tag. | ||
| * 7. An extra `]` to allow for escaping shortcodes with double `[[]]` | ||
| * | ||
| * @param tag Shortcode tag. | ||
| * | ||
| * @return Shortcode RegExp. | ||
| */ | ||
| regexp: ( tag: string ) => RegExp; | ||
|
|
||
| /** | ||
| * Replace matching shortcodes in a block of text. | ||
| * | ||
| * @param tag Shortcode tag. | ||
| * @param text Text to search. | ||
| * @param callback Function to process the match and return | ||
| * replacement string. | ||
| * | ||
| * @return Text with shortcodes replaced. | ||
| */ | ||
| replace: ( tag: string, text: string, callback: ReplaceCallback ) => string; | ||
|
|
||
| /** | ||
| * Generate a string from shortcode parameters. | ||
| * | ||
| * Creates a shortcode instance and returns a string. | ||
| * | ||
| * Accepts the same `options` as the `shortcode()` constructor, containing a | ||
| * `tag` string, a string or object of `attrs`, a boolean indicating whether to | ||
| * format the shortcode using a `single` tag, and a `content` string. | ||
| * | ||
| * @param options | ||
| * | ||
| * @return String representation of the shortcode. | ||
| */ | ||
| string: ( options: ShortcodeOptions ) => string; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "$schema": "https://json.schemastore.org/tsconfig.json", | ||
| "extends": "../../tsconfig.base.json", | ||
| "compilerOptions": { | ||
| "rootDir": "src", | ||
| "declarationDir": "build-types", | ||
| "checkJs": false | ||
| }, | ||
| "references": [], | ||
| "include": [ "src" ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.