Skip to content

Commit 23ba245

Browse files
committed
Merge branch 'v0'
2 parents 6c8b59b + 12d0949 commit 23ba245

File tree

4 files changed

+47
-10
lines changed

4 files changed

+47
-10
lines changed

packages/markdown-exit/src/common/utils.ts

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
/**
2-
* markdown-exit notes:
3-
* - drop re-export mdurl and ucmicro
4-
* - drop `assign` with `Object.assign`
5-
*/
6-
71
// Utilities
82
//
93

104
import { decodeHTML } from 'entities'
5+
import * as mdurl from 'mdurl'
116
import * as ucmicro from 'uc.micro'
127

138
const _hasOwnProperty = Object.prototype.hasOwnProperty
@@ -16,6 +11,31 @@ export function has(object: object, key: string | number | symbol): boolean {
1611
return _hasOwnProperty.call(object, key)
1712
}
1813

14+
// Utility: turn a union into an intersection (A | B) -> (A & B)
15+
type UnionToIntersection<U> =
16+
(U extends unknown ? (x: U) => void : never) extends (x: infer R) => void ? R : never
17+
18+
/**
19+
* Merge objects
20+
*/
21+
export function assign<
22+
T extends object,
23+
S extends readonly (object | null | undefined)[],
24+
>(target: T, ...sources: S): T & UnionToIntersection<NonNullable<S[number]>>
25+
26+
// Implementation signature
27+
export function assign(target: object, ...sources: unknown[]): any {
28+
for (const s of sources) {
29+
if (!s)
30+
continue
31+
if (typeof s !== 'object') {
32+
throw new TypeError('source must be object')
33+
}
34+
Object.assign(target, s as object)
35+
}
36+
return target
37+
}
38+
1939
// Remove element from array and put another array at those position.
2040
// Useful for some operations with tokens
2141
export function arrayReplaceAt<T>(src: readonly T[], pos: number, newElements: readonly T[]): T[] {
@@ -260,3 +280,10 @@ export function normalizeReference(str: string): string {
260280
export function isPromiseLike<T = unknown>(v: any): v is Promise<T> {
261281
return typeof v?.then === 'function'
262282
}
283+
284+
/**
285+
* Re-export libraries commonly used in both markdown-it and its plugins,
286+
* so plugins won't have to depend on them explicitly, which reduces their
287+
* bundled size (e.g. a browser build).
288+
*/
289+
export const lib = { mdurl, ucmicro }

packages/markdown-exit/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ export class MarkdownExit {
229229
* Link components parser functions, useful to write plugins. See details
230230
* [here](https://github.com/serkodev/markdown-exit/tree/main/packages/markdown-exit/src/helpers).
231231
*/
232-
helpers: typeof helpers = Object.assign({}, helpers)
232+
helpers = utils.assign({}, helpers)
233233

234234
options: Required<MarkdownExitOptions> = { ...config.default.options }
235235

@@ -267,7 +267,7 @@ export class MarkdownExit {
267267
* config.
268268
*/
269269
set(options: MarkdownExitOptions): this {
270-
Object.assign(this.options, options)
270+
utils.assign(this.options, options)
271271
return this
272272
}
273273

packages/markdown-exit/src/renderer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import type Token from './token'
1010
import type { HTMLAttribute } from './token'
1111
import type { MarkdownExitEnv } from './types/shared'
12-
import { escapeHtml, isPromiseLike, unescapeAll } from './common/utils'
12+
import { assign, escapeHtml, isPromiseLike, unescapeAll } from './common/utils'
1313

1414
export interface RenderOptions {
1515
/**
@@ -188,7 +188,7 @@ export default class Renderer {
188188
*
189189
* @see https://github.com/serkodev/markdown-exit/tree/main/packages/markdown-exit/src/renderer.ts
190190
*/
191-
rules: RenderRuleRecord = Object.assign({}, default_rules)
191+
rules: RenderRuleRecord = assign({}, default_rules)
192192

193193
/**
194194
* Creates new {@link Renderer} instance and fill {@link Renderer#rules} with defaults.

packages/markdown-exit/tests/utils.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ describe('utils', () => {
2323
assert.strictEqual(isValidEntityCode(0x7F), false)
2424
})
2525

26+
it('assign', () => {
27+
const assign = utils.assign
28+
29+
assert.deepEqual(assign({ a: 1 }, null, { b: 2 }), { a: 1, b: 2 })
30+
assert.throws(() => {
31+
// @ts-expect-error expect throw
32+
assign({}, 123)
33+
})
34+
})
35+
2636
it('escapeRE', () => {
2737
const escapeRE = utils.escapeRE
2838

0 commit comments

Comments
 (0)