Shared Methods
The following functions are available on both Node.js and Client.
These functions are available in @vuepress/helper, @vuepress/helper/client, and @vuepress/helper/shared.
Data Related
Encode/decode and zip/unzip data.
This is useful in markdown plugins when you want to encode string content and pass it to the component through props.
You may simply achieve this with encodeURIComponent and decodeURIComponent, but it can be very large if the content contains lots of special characters.
So we provide encodeData and decodeData to zip and encode content.
export const encodeData: (
data: string,
level: DeflateOptions['level'] = 6,
) => string
export const decodeData: (compressed: string) => stringDetails
const content = `
{
"type": "bar",
"data": {
"labels": ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
"datasets": [
{
"label": "# of Votes",
"data": [12, 19, 3, 5, 2, 3],
"backgroundColor": [
"rgba(255, 99, 132, 0.2)",
"rgba(54, 162, 235, 0.2)",
"rgba(255, 206, 86, 0.2)",
"rgba(75, 192, 192, 0.2)",
"rgba(153, 102, 255, 0.2)",
"rgba(255, 159, 64, 0.2)"
],
"borderColor": [
"rgba(255, 99, 132, 1)",
"rgba(54, 162, 235, 1)",
"rgba(255, 206, 86, 1)",
"rgba(75, 192, 192, 1)",
"rgba(153, 102, 255, 1)",
"rgba(255, 159, 64, 1)"
],
"borderWidth": 1
}
]
},
"options": {
"scales": {
"y": {
"beginAtZero": true
}
}
}
}
`
const prop = encodeData(content) // "eJyNUsFOwzAMve8rrHABKZqWlg5WxAE4cARxAMHEIV1NmQhNlaaCCe3fcdKtW0sLWGpjxy/v+UV512mlcIyfhTa2hHP4GgHYVYExsEQaxqlMpZWxbwAomaAqY5izO0wZB3apKnTrIyqlP1x2bRBzl9xWplC+eWNkniF7dmw1X4nWsfgaNtwNP2kfgH6Be22x9CPUUQ8yFwEHMeMQcog4UBFuiF0kcvGWGV3l6ZVW2uw0XDCTJfIwiOjYjAhESIcn4+BoT2MLio6pP6V+EBJ6AOSZgsmUwyl9A6ATwoiZn3lYTkTkRkycnuP8TU9ENPqUxuuA9i9BmxTNPy9A/G2/F9I23wtpW++FdIwPKzW2W5Afph+WqX2NQWz313XicT7XhV3qnB5f/ejKhVTYVACrXUqUmC3zC/uERsdgTYUdVr/Qb302+gZxe7S/"
decodeData(prop) // will be the original content
// if you use `encodeURIComponent`, it will be much longer
encodeURIComponent(content) // '%0A%7B%0A%20%20%22type%22%3A%20%22bar%22%2C%0A%20%20%22data%22%3A%20%7B%0A%20%20%20%20%22labels%22%3A%20%5B%22Red%22%2C%20%22Blue%22%2C%20%22Yellow%22%2C%20%22Green%22%2C%20%22Purple%22%2C%20%22Orange%22%5D%2C%0A%20%20%20%20%22datasets%22%3A%20%5B%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%22label%22%3A%20%22%23%20of%20Votes%22%2C%0A%20%20%20%20%20%20%20%20%22data%22%3A%20%5B12%2C%2019%2C%203%2C%205%2C%202%2C%203%5D%2C%0A%20%20%20%20%20%20%20%20%22backgroundColor%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%22rgba(255%2C%2099%2C%20132%2C%200.2)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%22rgba(54%2C%20162%2C%20235%2C%200.2)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%22rgba(255%2C%20206%2C%2086%2C%200.2)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%22rgba(75%2C%20192%2C%20192%2C%200.2)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%22rgba(153%2C%20102%2C%20255%2C%200.2)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%22rgba(255%2C%20159%2C%2064%2C%200.2)%22%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%22borderColor%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%22rgba(255%2C%2099%2C%20132%2C%201)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%22rgba(54%2C%20162%2C%20235%2C%201)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%22rgba(255%2C%20206%2C%2086%2C%201)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%22rgba(75%2C%20192%2C%20192%2C%201)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%22rgba(153%2C%20102%2C%20255%2C%201)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%22rgba(255%2C%20159%2C%2064%2C%201)%22%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%22borderWidth%22%3A%201%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%5D%0A%20%20%7D%2C%0A%20%20%22options%22%3A%20%7B%0A%20%20%20%20%22scales%22%3A%20%7B%0A%20%20%20%20%20%20%22y%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22beginAtZero%22%3A%20true%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%0A'Type Helper
isDef(x): Check ifxis defined.isBoolean(x): Check ifxis a boolean.isString(x): Check ifxis a string.isNumber(x): Check ifxis a number.isPlainObject(x): Check ifxis a plain object.isArray(x): Check ifxis an array.isFunction(x): Check ifxis a function.isRegExp(x): Check ifxis a regular expression.
String Related
startsWith(a, b): Check if stringastarts with stringb.endsWith(a, b): Check if stringaends with stringb.
Return false if a is not a string.
Object Related
keys(x): Return an array of keys of objectx.values(x): Return an array of values of objectx.entries(x): Convert objectxto an array of key-value pairs.fromEntries(x): Convert an array of key-value pairsxto an object.deepAssign(x, y, ...): A deep version ofObject.assign.Example
// or @vuepress/helper/client import { deepAssign } from '@vuepress/helper' const defaultOptions = { optionA: { optionA1: 'defaultOptionA1', optionA2: 'defaultOptionA2', optionA3: 'defaultOptionA3', }, optionB: true, optionC: 'optionC', } const userOptions = { optionA: { optionA1: 'optionA1', optionA2: 'optionA2', }, optionB: false, } deepAssign(defaultOptions, userOptions) // { // optionA: { // optionA1: "optionA1", // optionA2: "optionA2", // optionA3: "defaultOptionA3", // }, // optionB: false, // optionC: "optionC", // }
Date Related
getDate(x): Convert inputxto a date. It can supportDate, timestamp, and date string. The support range of date string depends on theDate.parsesupport range of the environment. Returnnullwhen it cannot be converted to a date.Example
getDate('2021-01-01') // a Date object represents 2021-01-01 getDate(1609459200000) // a Date object represents 2021-01-01 getDate('2021-01-01T00:00:00.000Z') // a Date object represents 2021-01-01 getDate('2021/01/01') // a Date object represents 2021-01-01 (might be null in some browsers) getDate('invalid date') // null getDate(undefined) // null getDate(-32) // nulldateSorter: Sort the values that can be converted to dates from new to old, and the values that cannot be converted to dates will be at the end.Example
const arr = [ '2020-01-01', 1609459200000, '2022-01-01T00:00:00.000Z', '2023/01/01', 'invalid date', undefined, -32, ] arr.sort(dateSorter) // [ // '2023/01/01', // '2022-01-01T00:00:00.000Z', // 1609459200000, // '2020-01-01', // 'invalid date', // undefined, // -32, // ]
Link Related
isLinkHttp(x): Check ifxis a valid HTTP URL.isLinkWithProtocol(x): Check ifxis a valid URL with protocol.isLinkExternal(x): Check ifxis a valid external URL.isLinkAbsolute(x): Check ifxis a valid absolute URL.isLinkRelative(x): Check ifxis not absolute or external URL.ensureEndingSlash(x): Ensurexends with a slash.ensureLeadingSlash(x): Ensurexstarts with a slash.removeEndingSlash(x): Ensurexdoes not end with a slash.removeLeadingSlash(x): Ensurexdoes not start with a slash.
