Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | 1x 65x 67x 1x 19x 19x 31x 31x 29x 16x 13x 1x 1x 143x 143x 144x 144x 144x 5x 2x 139x 87x 36x 36x 61x 61x 61x 11x 50x 50x 50x 2x 1x 66x 66x 1x 65x 65x 65x 65x 65x 67x 1x 66x 1x 65x 6x 65x 1x 61x 7x 54x 51x 3x | import type { Element, Comment, Literal, ElementContent, RootContent, Properties } from 'hast';
import type { RehypeAttrsOptions } from './index.js';
export const getURLParameters = (url: string = '') =>
((url.match(/([^?=&]+)(=([^&]*))/g) || []) as string[]).reduce(
(a: Record<string, string | number | boolean>, v: string) => (
(a[v.slice(0, v.indexOf('=')) as keyof typeof a] = v.slice(v.indexOf('=') + 1)), a
),
{}
)
export const prevChild = (data: Literal[] = [], index: number): Comment | undefined => {
let i = index;
while (i > -1) {
i--;
if (!data[i]) return
if ((data[i] && data[i].value && (data[i].value as string).replace(/(\n|\s)/g, '') !== '') || data[i].type !== 'text') {
if (!/^rehype:/.test(data[i].value as string) || (data[i].type as string) !== 'comment') return;
return data[i] as unknown as Comment;
}
}
return;
}
export const nextChild = (data: RootContent[] | ElementContent[] = [], index: number, tagName?: string, commentStart: string = "<!--", commentEnd: string = "-->"): ElementContent | undefined => {
let i = index;
while (i < data.length) {
i++;
const element = data[i] as Literal & Element;
if (tagName) {
if (element && element.value && (element.value as string).replace(/(\n|\s)/g, '') !== '' || data[i] && (data[i].type as string) === 'element') {
return element.tagName === tagName ? element : undefined
}
} else {
if (!element || element.type === 'element') return;
if (element.type === 'text') {
const nextNode = nextChild(data, i, undefined)
if (nextNode) return nextNode;
};
Eif (element.type && /^(comment|raw|text)$/ig.test(element.type)) {
const regx = new RegExp(`^(\s+)?${commentStart}(.*?)${commentEnd}`);
if (element.value && !/^rehype:/.test(element.value.replace(/^(\n|\s)+/, '').replace(regx, '$2') || '')) {
return
};
let comment = element.value.replace(/^(\n|\s)+/, '');
element.value = comment
return element;
}
}
}
return
}
/**
* Get the position of the code comment
* @param data Comment
* @param start
* @param end
* @returns Returns the current parameter data Object, `{}`
*/
export const getCommentObject = ({ value = '' }: Comment, start: string = "<!--", end: string = "-->"): Properties => {
let regx: RegExp;
try {
// Construct a regular expression to match the comment content
regx = new RegExp(`^${start}(.*?)${end}`);
} catch (error) {
return {};
}
const match = value.match(regx);
const content = (match ? match[1] : value)
// Extract the comment content if it matches the regular expression
const commentContent = content.replace(/^rehype:/, '');
// Extract the comment content and parse it into a parameter object
const param = getURLParameters(commentContent);
// Iterate over the key-value pairs of the parameter object and perform type conversion
Object.keys(param).forEach((keyName: string) => {
if (param[keyName] === 'true') {
param[keyName] = true;
} else if (param[keyName] === 'false') {
param[keyName] = false;
} else if (typeof param[keyName] === 'string' && !/^0/.test(param[keyName] as string) && !isNaN(+param[keyName])) {
param[keyName] = +param[keyName];
}
});
// Return the processed parameter object
return param;
}
export type DataConfig = {
'data-config': Properties
}
export const propertiesHandle = (defaultAttrs?: Properties | null, attrs?: Properties, type?: RehypeAttrsOptions['properties']): Properties | DataConfig => {
if (type === 'string') {
return { ...defaultAttrs, 'data-config': JSON.stringify({ ...attrs, rehyp: true })}
} else if (type === 'attr') {
return { ...defaultAttrs, ...attrs}
}
return { ...defaultAttrs, 'data-config': { ...attrs, rehyp: true }}
} |