Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/typed-engine-constructor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'cherry-markdown': patch
---

fix: declare CherryEngine's constructor result as Engine while retaining its static APIs
6 changes: 4 additions & 2 deletions packages/cherry-markdown/src/index.engine.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ class CherryEngine extends CherryStatic {
export { SyntaxHookBase, MenuHookBase };

/**
* @typedef {typeof CherryStatic & (new (options: Partial<import('~types/cherry').CherryOptions>) => Engine)}
* Preserve the static API while declaring the Engine instance returned by the
* compatibility constructor. A class declaration alone cannot express this.
* @typedef {Omit<typeof CherryEngine, 'prototype'> & (new (options?: ConstructorParameters<typeof CherryEngine>[0]) => Engine)} CherryEngineConstructor
*/
const CherryEngineExport = CherryEngine;
const CherryEngineExport = /** @type {CherryEngineConstructor} */ (/** @type {unknown} */ (CherryEngine));

export default CherryEngineExport;
1 change: 0 additions & 1 deletion packages/cherry-markdown/test/Previewer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { createPreviewer } from './helpers/previewer';
type EngineOptions = ConstructorParameters<typeof CherryEngine>[0];

function createEngine(options: EngineOptions = {}): Engine {
// @ts-expect-error CherryEngine's compatibility constructor returns an Engine instance.
return new CherryEngine(options);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ describe('addons/combined Markdown rendering', () => {
PlantUMLCodeEngine.install(options, {});
const engine = new CherryEngine(options);
const container = document.createElement('div');
// @ts-expect-error CherryEngine's compatibility constructor returns an Engine instance.
container.innerHTML = engine.makeHtml(`
# Release diagrams

Expand Down Expand Up @@ -95,7 +94,6 @@ $$
},
});
const container = document.createElement('div');
// @ts-expect-error CherryEngine's compatibility constructor returns an Engine instance.
container.innerHTML = engine.makeHtml('## Fallback\n\n```mermaid\ngraph TD; A-->B\n```\n\nAfter the diagram.');
const codeBlock = container.querySelector('[data-type="codeBlock"][data-lang="mermaid"]');

Expand Down
2 changes: 0 additions & 2 deletions packages/cherry-markdown/test/addons/echarts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,9 @@ describe('addons/EChartsCodeBlockEngine', () => {
const engine = new CherryEngine({
engine: { syntax: { codeBlock: { customRenderer: { echarts: renderer } } } },
});
// @ts-expect-error CherryEngine's compatibility constructor returns an Engine instance.
Object.defineProperty(engine.$cherry, 'previewer', {
value: { getDom: () => environment.previewerDom },
});
// @ts-expect-error CherryEngine's compatibility constructor returns an Engine instance.
const html = engine.makeHtml('```echarts\n{ series: [{ type: "bar", data: [3, 5] }] }\n```');
environment.previewerDom.innerHTML = html;
const wrapper = environment.previewerDom.querySelector('[data-type="echarts"]');
Expand Down
1 change: 0 additions & 1 deletion packages/cherry-markdown/test/addons/mermaid.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,6 @@ describe('addons/MermaidCodeEngine', () => {
MermaidCodeEngine.install(options, { mermaidAPI: { initialize, render } });
const engine = new CherryEngine(options);
const container = document.createElement('div');
// @ts-expect-error CherryEngine's compatibility constructor returns an Engine instance.
container.innerHTML = engine.makeHtml('```mermaid\ngraph TD; A-->B\n```');
const figure = container.querySelector('figure[data-type="mermaid"]');
const svg = figure?.querySelector('svg');
Expand Down
1 change: 0 additions & 1 deletion packages/cherry-markdown/test/addons/plantuml.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ describe('addons/PlantUMLCodeEngine', () => {
PlantUMLCodeEngine.install(options, {});
const engine = new CherryEngine(options);
const container = document.createElement('div');
// @ts-expect-error CherryEngine's compatibility constructor returns an Engine instance.
container.innerHTML = engine.makeHtml('```plantuml\n@startuml\nAlice -> Bob: Hello\n@enduml\n```');
const wrapper = container.querySelector('[data-type="plantuml"]');
const image = wrapper?.querySelector('img');
Expand Down
1 change: 0 additions & 1 deletion packages/cherry-markdown/test/core/CommonMark.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ describe('engine', () => {
vi.stubGlobal('BUILD_ENV', 'production');
});
it.each(suites)('CommonMark-$example', (item) => {
// @ts-expect-error CherryEngine 构造器实际返回 Engine 实例
const cherryHtml = cherryEngine.makeHtml(String(item.markdown));

expect(cherryHtml).toMatchSnapshot();
Expand Down
1 change: 0 additions & 1 deletion packages/cherry-markdown/test/core/Engine.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import hooksConfig from '../../src/core/HooksConfig';
type EngineOptions = ConstructorParameters<typeof CherryEngine>[0];

function createEngine(options: EngineOptions = {}): Engine {
// @ts-expect-error CherryEngine's compatibility constructor returns an Engine instance.
return new CherryEngine(options);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import Engine from '../../src/Engine';
type EngineOptions = ConstructorParameters<typeof CherryEngine>[0];

function createEngine(options: EngineOptions = {}): Engine {
// @ts-expect-error CherryEngine's compatibility constructor returns an Engine instance.
return new CherryEngine(options);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const createEngine = (options = {}) =>
const render = (markdown: string, options = {}) => {
const engine = createEngine(options);
const container = document.createElement('div');
// @ts-expect-error CherryEngine's compatibility constructor returns an Engine instance.
container.innerHTML = engine.makeHtml(markdown);
return container;
};
Expand Down
Loading