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
2 changes: 1 addition & 1 deletion packages/muya/src/__tests__/frontMatter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import type Content from '../block/base/content';
import type Parent from '../block/base/parent';
import type { IFrontmatterState } from '../state/types';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { replaceBlockByLabel } from '../block/blockTransforms';
import { Muya } from '../muya';
import { replaceBlockByLabel } from '../ui/paragraphQuickInsertMenu/config';

// Coverage for the two front-matter entry points — the desktop Paragraph >
// Front Matter menu item (`muya.updateParagraph('front-matter')`) and the
Expand Down
69 changes: 69 additions & 0 deletions packages/muya/src/block/base/__tests__/setextHeadingLevel.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// @vitest-environment happy-dom

import type { Muya } from '../../../muya';
import type SetextHeading from '../../commonMark/setextHeading';
import type Format from '../format';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { Muya as MuyaClass } from '../../../muya';

// Typing a setext underline must produce the CommonMark level: `===` → level 1
// (h1), `---` → level 2 (h2). Regression: `_convertToSetextHeading` inverted
// the test (`/=/ ? 2 : 1`), so `===` rendered as an h2 until a source-mode
// round-trip re-parsed it.

vi.mock('../../../utils/prism/index', () => ({
default: {},
walkTokens: () => null,
loadedLanguages: new Set(),
transformAliasToOrigin: (s: string) => s,
loadLanguage: () => Promise.resolve([]),
search: () => [],
}));

const bootedHosts: HTMLElement[] = [];

beforeEach(() => {
window.MUYA_VERSION = 'test';
});

afterEach(() => {
while (bootedHosts.length)
bootedHosts.pop()!.remove();
delete (window as Partial<Window>).MUYA_VERSION;
});

function bootMuya(markdown: string): Muya {
const host = document.createElement('div');
document.body.appendChild(host);
const muya = new MuyaClass(host, { markdown } as ConstructorParameters<typeof MuyaClass>[1]);
muya.init();
bootedHosts.push(muya.domNode);
return muya;
}

function convertViaUnderline(underline: string): SetextHeading {
const muya = bootMuya('hello world\n');
const content = muya.editor.scrollPage!.firstContentInDescendant() as Format;
content.text = `hello world\n${underline}`;
content.checkInlineUpdate();

return muya.editor.scrollPage!.firstChild as unknown as SetextHeading;
}

describe('setext heading conversion uses the CommonMark level', () => {
it('`===` underline creates a level-1 heading (h1)', () => {
const heading = convertViaUnderline('===');

expect(heading.blockName).toBe('setext-heading');
expect(heading.meta.level).toBe(1);
expect(heading.tagName).toBe('h1');
});

it('`---` underline creates a level-2 heading (h2)', () => {
const heading = convertViaUnderline('---');

expect(heading.blockName).toBe('setext-heading');
expect(heading.meta.level).toBe(2);
expect(heading.tagName).toBe('h2');
});
});
42 changes: 1 addition & 41 deletions packages/muya/src/block/base/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,6 @@ function shouldInsertClosingPair(
);
}

const FLOAT_PREVENT_DEFAULT_NAMES = new Set([
'mu-format-picker',
'mu-table-picker',
'mu-quick-insert',
'mu-emoji-picker',
'mu-front-menu',
'mu-list-picker',
'mu-image-selector',
'mu-table-column-tools',
'mu-table-bar-tools',
]);

interface IAutoPairCollapsedContext {
blockText: string;
options: {
Expand Down Expand Up @@ -676,8 +664,7 @@ class Content extends TreeNode {
if (!isKeyboardEvent(event))
return;

// TODO: move codes bellow to muya.ui ?
if (this._handleShownFloatKeydown(event))
if (this.muya.ui.handleContentKeydown(event))
return;

switch (event.key) {
Expand Down Expand Up @@ -715,33 +702,6 @@ class Content extends TreeNode {
}
};

private _handleShownFloatKeydown(event: KeyboardEvent): boolean {
if (
this.muya.ui.shownFloat.size > 0
&& (event.key === EVENT_KEYS.Enter
|| event.key === EVENT_KEYS.Escape
|| event.key === EVENT_KEYS.Tab
|| event.key === EVENT_KEYS.ArrowUp
|| event.key === EVENT_KEYS.ArrowDown)
) {
let needPreventDefault = false;

for (const tool of this.muya.ui.shownFloat) {
if (FLOAT_PREVENT_DEFAULT_NAMES.has(tool.name)) {
needPreventDefault = true;
break;
}
}

if (needPreventDefault)
event.preventDefault();

return true;
}

return false;
}

blurHandler() {
this.scrollPage?.handleBlurFromContent(this);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/muya/src/block/base/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
'(?:^|\n) {0,3}((?:\\* *\\* *\\*|- *- *-|_ *_ *_)[ *_-]*)(?=\n|$)', // Thematic break
];

const INLINE_UPDATE_REG = new RegExp(INLINE_UPDATE_FRAGMENTS.join('|'), 'i');

Check warning on line 59 in packages/muya/src/block/base/format.ts

View workflow job for this annotation

GitHub Actions / lint

The quantified expression ' +' at the end of the expression tree should only be matched a constant number of times. The expression can be replaced with ' ' (no quantifier) without affecting the lookaround

Check warning on line 59 in packages/muya/src/block/base/format.ts

View workflow job for this annotation

GitHub Actions / lint

The quantified expression '\s+' at the end of the expression tree should only be matched a constant number of times. The expression can be replaced with '\s' (no quantifier) without affecting the lookaround

// Offset of the cursor relative to a symmetric/asymmetric marker pair
// (strong/em/code/math/html_tag). `open`/`close` are the opening/closing
Expand Down Expand Up @@ -999,7 +999,7 @@
let atxLineHasPushed = false;

for (const l of lines) {
if (/^ {0,3}#{1,6}(?=\s+|$)/.test(l) && !atxLineHasPushed) {

Check warning on line 1002 in packages/muya/src/block/base/format.ts

View workflow job for this annotation

GitHub Actions / lint

The quantified expression '\s+' at the end of the expression tree should only be matched a constant number of times. The expression can be replaced with '\s' (no quantifier) without affecting the lookaround
atxLine = l;
atxLineHasPushed = true;
}
Expand Down Expand Up @@ -1062,7 +1062,7 @@

// Setext Heading
private _convertToSetextHeading(setextHeading: string) {
const level = /=/.test(setextHeading) ? 2 : 1;
const level = /=/.test(setextHeading) ? 1 : 2;
if (
this.parent?.blockName === 'setext-heading'
&& (this.parent as SetextHeading).meta.level === level
Expand All @@ -1078,7 +1078,7 @@
let setextLineHasPushed = false;

for (const l of lines) {
if (/^ {0,3}(?:={3,}|-{3,})(?= +|$)/.test(l) && !setextLineHasPushed)

Check warning on line 1081 in packages/muya/src/block/base/format.ts

View workflow job for this annotation

GitHub Actions / lint

The quantified expression ' +' at the end of the expression tree should only be matched a constant number of times. The expression can be replaced with ' ' (no quantifier) without affecting the lookaround
setextLineHasPushed = true;
else if (!setextLineHasPushed)
setextLines.push(l);
Expand Down
Loading
Loading