Skip to content
Merged
2 changes: 2 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { MarkEdit } from 'markedit-api';
import { proofreadingExtension } from './src/extension';
import { buildMenuItem } from './src/menu';

MarkEdit.addExtension(proofreadingExtension());
MarkEdit.addMainMenuItem(buildMenuItem());
1 change: 1 addition & 0 deletions src/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const repoUrl = 'https://github.com/MarkEdit-app/MarkEdit-proofreading';
48 changes: 48 additions & 0 deletions src/menu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { MarkEdit } from 'markedit-api';
import type { MenuItem } from 'markedit-api';
import { setDiagnosticsEffect, lintToDiagnostic } from './decoration';
import { lint } from './lint';
import { repoUrl } from './const';

export function buildMenuItem(): MenuItem {
return {
title: 'Proofread',
icon: 'text.badge.checkmark',
children: [
{
title: 'Proofread Now',
action: proofreadNow,
},
{
title: 'Ignore All',
action: ignoreAll,
},
{ separator: true },
{
title: `Version ${__PKG_VERSION__}`,
action: () => open(`${repoUrl}/releases/tag/v${__PKG_VERSION__}`),
},
Comment thread
cyanzhong marked this conversation as resolved.
{
title: 'Check Release (GitHub)',
action: () => open(`${repoUrl}/releases`),
},
],
};
}

async function proofreadNow() {
const view = MarkEdit.editorView;
const doc = view.state.doc;
const text = doc.sliceString(0);
const lints = await lint(text);

if (view.state.doc !== doc) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot What are you doing here? Is this really a needed pattern??

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the stale document check in 70b343d. The lintScheduler in extension.ts already handles auto-refresh on doc changes, so this guard isn't needed for a manual user action.

return;
}

view.dispatch({ effects: setDiagnosticsEffect.of(lints.map(lintToDiagnostic)) });
}

function ignoreAll() {
MarkEdit.editorView.dispatch({ effects: setDiagnosticsEffect.of([]) });
}
28 changes: 28 additions & 0 deletions tests/menu.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { describe, expect, it } from 'vitest';
import { buildMenuItem } from '../src/menu';

describe('buildMenuItem', () => {
it('returns the expected menu structure', () => {
const item = buildMenuItem();

expect(item.title).toBe('Proofread');
expect(item.icon).toBe('text.badge.checkmark');

const children = item.children!;
expect(children).toHaveLength(5);

expect(children[0].title).toBe('Proofread Now');
expect(typeof children[0].action).toBe('function');

expect(children[1].title).toBe('Ignore All');
expect(typeof children[1].action).toBe('function');

expect(children[2].separator).toBe(true);

expect(children[3].title).toMatch(/^Version \S+/);
expect(typeof children[3].action).toBe('function');

Comment thread
cyanzhong marked this conversation as resolved.
expect(children[4].title).toBe('Check Release (GitHub)');
expect(typeof children[4].action).toBe('function');
});
});
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"strictNullChecks": true,
"importHelpers": true,
"noEmit": true,
"skipLibCheck": true
"skipLibCheck": true,
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true
}
}
4 changes: 4 additions & 0 deletions types/globals.d.ts
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Move this to types instead of src.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to types/globals.d.ts in bb42c7c.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Package version read from the `package.json` file.
*/
declare const __PKG_VERSION__: string;
4 changes: 4 additions & 0 deletions vite.config.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { defineConfig, mergeConfig } from 'vite';
import { viteSingleFile } from 'vite-plugin-singlefile';
import { defaultViteConfig } from 'markedit-vite';
import mainPackage from './package.json' with { type: 'json' };

export default defineConfig(mergeConfig(defaultViteConfig(), {
define: {
__PKG_VERSION__: JSON.stringify(mainPackage.version),
},
plugins: [viteSingleFile()],
}));