/
githubmirror
/
marktext
Обзор
Документация
Войти
/
githubmirror
/
marktext
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
packages/muya/src/utils/__tests__/pasteUrlTitleEntities.spec.ts
48 строк
2 KB
Ran Luo
fix(muya): decode HTML entities in a fetched page title when pasting a URL (#2525) (#4729)
26 июн 2026, 09:27
Не верифицирован
26 июн 2026, 09:27
0823599
Код
Авторство
О чём код?
// @vitest-environment jsdom import { afterEach, describe, expect, it, vi } from 'vitest'; import { getPageTitle, normalizePastedHTML } from '../paste'; // #2525 — copying a bare URL from a browser pastes a markdown link whose anchor // text is the page `<title>`. The title is fetched as raw HTML, so entities such // as `–` / `ü` must be decoded before they become the link text; // otherwise the literal `–` shows up in the document. function mockFetchReturningTitle(titleInnerHtml: string) { const body = `<!doctype html><html><head><title>${titleInnerHtml}</title></head><body>x</body></html>`; return vi.fn(async () => ({ status: 200, headers: { get: (h: string) => (h.toLowerCase() === 'content-type' ? 'text/html; charset=utf-8' : null), }, text: async () => body, })); } afterEach(() => { vi.unstubAllGlobals(); vi.restoreAllMocks(); }); describe('getPageTitle decodes HTML entities (#2525)', () => { it('decodes named entities in the fetched page title', async () => { vi.stubGlobal( 'fetch', mockFetchReturningTitle('On Statistical Data Compression – Digitale Bibliothek Thüringen'), ); const title = await getPageTitle('https://www.db-thueringen.de/receive/dbt_mods_00027239'); expect(title).toBe('On Statistical Data Compression – Digitale Bibliothek Thüringen'); }); it('a pasted bare URL becomes a link whose anchor text has decoded entities', async () => { vi.stubGlobal('fetch', mockFetchReturningTitle('Foo – Bücher')); const out = await normalizePastedHTML( '<a href="https://www.db-thueringen.de/x">https://www.db-thueringen.de/x</a>', ); expect(out).toContain('Foo – Bücher'); expect(out).not.toContain('ndash'); }); });