-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Convert autop package to TypeScript #61573
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
* | ||
* @type {RegExp} | ||
*/ | ||
const htmlSplitRegex = ( () => { | ||
const htmlSplitRegex: RegExp = ( () => { | ||
/* eslint-disable no-multi-spaces */ | ||
const comments = | ||
'!' + // Start of comment, after the <. | ||
|
@@ -55,7 +55,7 @@ const htmlSplitRegex = ( () => { | |
* | ||
* @return {string[]} The formatted text. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, we only need to document the params, the types are on the function signature:
|
||
*/ | ||
function htmlSplit( input ) { | ||
function htmlSplit( input: string ): string[] { | ||
const parts = []; | ||
let workingInput = input; | ||
|
||
|
@@ -65,7 +65,7 @@ function htmlSplit( input ) { | |
// If the `g` flag is omitted, `index` is included. | ||
// `htmlSplitRegex` does not have the `g` flag so we can assert it will have an index number. | ||
// Assert `match.index` is a number. | ||
const index = /** @type {number} */ ( match.index ); | ||
const index = match.index!; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't use the |
||
|
||
parts.push( workingInput.slice( 0, index ) ); | ||
parts.push( match[ 0 ] ); | ||
|
@@ -87,7 +87,10 @@ function htmlSplit( input ) { | |
* | ||
* @return {string} The formatted text. | ||
*/ | ||
function replaceInHtmlTags( haystack, replacePairs ) { | ||
function replaceInHtmlTags( | ||
haystack: string, | ||
replacePairs: Record< string, string > | ||
): string { | ||
// Find all elements. | ||
const textArr = htmlSplit( haystack ); | ||
let changed = false; | ||
|
@@ -137,8 +140,8 @@ function replaceInHtmlTags( haystack, replacePairs ) { | |
* | ||
* @return {string} Text which has been converted into paragraph tags. | ||
*/ | ||
export function autop( text, br = true ) { | ||
const preTags = []; | ||
export function autop( text: string, br: boolean = true ): string { | ||
const preTags: Array< [ string, string ] > = []; | ||
|
||
if ( text.trim() === '' ) { | ||
return ''; | ||
|
@@ -340,13 +343,12 @@ export function autop( text, br = true ) { | |
* | ||
* @return {string} The content with stripped paragraph tags. | ||
*/ | ||
export function removep( html ) { | ||
export function removep( html: string ): string { | ||
const blocklist = | ||
'blockquote|ul|ol|li|dl|dt|dd|table|thead|tbody|tfoot|tr|th|td|h[1-6]|fieldset|figure'; | ||
const blocklist1 = blocklist + '|div|p'; | ||
const blocklist2 = blocklist + '|pre'; | ||
/** @type {string[]} */ | ||
const preserve = []; | ||
const preserve: string[] = []; | ||
let preserveLinebreaks = false; | ||
let preserveBr = false; | ||
|
||
|
@@ -480,7 +482,7 @@ export function removep( html ) { | |
// Restore preserved tags. | ||
if ( preserve.length ) { | ||
html = html.replace( /<wp-preserve>/g, () => { | ||
return /** @type {string} */ ( preserve.shift() ); | ||
return preserve.shift()!; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here also |
||
} ); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove all the JSDoc type anottations now, they are duplicate and redundant. The
docgen
tool that generates the Markdown documentation will retrieve the types from the code, and should generate exactly the same output.