Skip to content

Commit a7e4bbc

Browse files
committed
fix: polyfill flatMap
1 parent 5d746df commit a7e4bbc

File tree

4 files changed

+63
-49
lines changed

4 files changed

+63
-49
lines changed

src/plugins/prism.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { JSItem, CSSItem } from '../types';
2+
import { arrayFrom, flatMap } from '../util';
23

34
export const styles: CSSItem[] = [
45
{
@@ -32,9 +33,9 @@ export const scripts: JSItem[] = [
3233
},
3334
];
3435

35-
export function transform(nodes, mm): void {
36+
export function transform(nodes: HTMLElement[], mm): void {
3637
const { Prism } = window as any;
37-
const langs = nodes.flatMap(node => Array.from(node.querySelectorAll('code[class*=language-]')))
38+
const langs = flatMap(nodes, node => arrayFrom(node.querySelectorAll('code[class*=language-]')))
3839
.map(code => {
3940
const lang = code.className.match(/(?:^|\s)language-(\S+)|$/)[1];
4041
if (Prism.languages[lang]) {

src/util/base.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const uniqId = Math.random().toString(36).slice(2, 8);
2+
let globalIndex = 0;
3+
export function getId(): string {
4+
globalIndex += 1;
5+
return `mm-${uniqId}-${globalIndex}`;
6+
}
7+
8+
export function walkTree<T>(tree: T, callback: (item: T, next: () => void, parent?: T) => void, key = 'c'): void {
9+
const walk = (item: T, parent?: T): void => callback(item, () => {
10+
item[key]?.forEach((child: T) => {
11+
walk(child, item);
12+
});
13+
}, parent);
14+
walk(tree);
15+
}
16+
17+
export function arrayFrom<T>(arrayLike: ArrayLike<T>): T[] {
18+
if (Array.from) return Array.from(arrayLike);
19+
const array = [];
20+
for (let i = 0; i < arrayLike.length; i += 1) {
21+
array.push(arrayLike[i]);
22+
}
23+
return array;
24+
}
25+
26+
export function flatMap<T, U>(arrayLike: ArrayLike<T>, callback: (item?: T, index?: number, thisObj?: ArrayLike<T>) => U | U[]): U[] {
27+
if ((arrayLike as Array<T>).flatMap) return (arrayLike as Array<T>).flatMap(callback);
28+
const array = [];
29+
for (let i = 0; i < arrayLike.length; i += 1) {
30+
const result = callback(arrayLike[i], i, arrayLike);
31+
if (Array.isArray(result)) array.push(...result);
32+
else array.push(result);
33+
}
34+
return array;
35+
}
36+
37+
export function addClass(className: string, ...rest: string[]): string {
38+
const classList = (className || '').split(' ').filter(Boolean);
39+
rest.forEach(item => {
40+
if (item && classList.indexOf(item) < 0) classList.push(item);
41+
});
42+
return classList.join(' ');
43+
}
44+
45+
export function childSelector<T extends Element>(filter?: string | ((el: T) => boolean)): () => T[] {
46+
if (typeof filter === 'string') {
47+
const tagName = filter;
48+
filter = (el: T): boolean => el.tagName === tagName;
49+
}
50+
const filterFn = filter;
51+
return function selector(): T[] {
52+
let nodes = arrayFrom((this as HTMLElement).childNodes as NodeListOf<T>);
53+
if (filterFn) nodes = nodes.filter(node => filterFn(node));
54+
return nodes;
55+
};
56+
}

src/util/index.ts

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,3 @@
1+
export * from './base';
12
export * from './html';
23
export * from './loader';
3-
4-
const uniqId = Math.random().toString(36).slice(2, 8);
5-
let globalIndex = 0;
6-
export function getId(): string {
7-
globalIndex += 1;
8-
return `mm-${uniqId}-${globalIndex}`;
9-
}
10-
11-
export function walkTree<T>(tree: T, callback: (item: T, next: () => void, parent?: T) => void, key = 'c'): void {
12-
const walk = (item: T, parent?: T): void => callback(item, () => {
13-
item[key]?.forEach((child: T) => {
14-
walk(child, item);
15-
});
16-
}, parent);
17-
walk(tree);
18-
}
19-
20-
export function arrayFrom<T>(arrayLike: ArrayLike<T>): T[] {
21-
const array = [];
22-
for (let i = 0; i < arrayLike.length; i += 1) {
23-
array.push(arrayLike[i]);
24-
}
25-
return array;
26-
}
27-
28-
export function addClass(className: string, ...rest: string[]): string {
29-
const classList = (className || '').split(' ').filter(Boolean);
30-
rest.forEach(item => {
31-
if (item && classList.indexOf(item) < 0) classList.push(item);
32-
});
33-
return classList.join(' ');
34-
}
35-
36-
export function childSelector<T extends Element>(filter?: string | ((el: T) => boolean)): () => T[] {
37-
if (typeof filter === 'string') {
38-
const tagName = filter;
39-
filter = (el: T): boolean => el.tagName === tagName;
40-
}
41-
const filterFn = filter;
42-
return function selector(): T[] {
43-
let nodes = arrayFrom((this as HTMLElement).childNodes as NodeListOf<T>);
44-
if (filterFn) nodes = nodes.filter(node => filterFn(node));
45-
return nodes;
46-
};
47-
}

src/util/loader.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { JSItem, JSScriptItem, CSSItem, IMarkmapPlugin } from '../types';
22
import { escapeScript, wrapHtml } from './html';
3+
import { flatMap } from './base';
34

45
export function buildCode(fn: Function, ...args: any[]): string {
56
const params = args.map(arg => {
@@ -121,8 +122,8 @@ export function persistCSS(items: CSSItem[]): string[] {
121122
}
122123

123124
export function persistPlugins(plugins: IMarkmapPlugin[], context?: any) {
124-
const js = plugins.flatMap(plugin => persistJS(plugin.scripts, context)).join('');
125-
const css = plugins.flatMap(plugin => persistCSS(plugin.styles)).join('');
125+
const js = flatMap(plugins, plugin => persistJS(plugin.scripts, context)).join('');
126+
const css = flatMap(plugins, plugin => persistCSS(plugin.styles)).join('');
126127
const processors = plugins.map(({ transform }) => transform);
127128
return {
128129
js,

0 commit comments

Comments
 (0)