import {expect} from '@open-wc/testing'; import {htmlToRemarkd, pastedRemarkd, plainTextToRemarkd} from './paste'; import {parse} from 'remarkd-js'; function clipboard(data: Record): DataTransfer { const transfer = new DataTransfer(); for (const [type, value] of Object.entries(data)) transfer.setData(type, value); return transfer; } describe('htmlToRemarkd', () => { it('should keep nested bullet and numbered lists', () => { expect(htmlToRemarkd('')) .to.equal('- a\n-- b\n--- c\n- d'); expect(htmlToRemarkd('
  1. a
  2. b
    1. c
    2. d
  3. e
')) .to.equal('1. a\n2. b\n2.1. c\n2.2. d\n3. e'); expect(htmlToRemarkd('
  1. four
')).to.equal('4. four'); }); it('should nest numbered and bullet lists inside each other', () => { expect(htmlToRemarkd('
  1. a
    • b
')).to.equal('1. a\n- b'); expect(htmlToRemarkd('')).to.equal('- a\n1. b'); }); it('should keep a list item on one line', () => { expect(htmlToRemarkd('')) .to.equal('- first second\n- line break'); }); it('should keep checkboxes in list items', () => { expect(htmlToRemarkd('')) .to.equal('- [x] done\n- [ ] todo'); }); it('should keep headings, paragraphs and inline formatting', () => { expect(htmlToRemarkd('

Title

Some bold, italic, code, gone and ' + 'a link.

Next

')) .to.equal('## Title\n\nSome **bold**, __italic__, `code`, ~~gone~~ and [a link](https://example.com).\n\nNext'); }); it('should keep the spaces around a mark outside it', () => { expect(htmlToRemarkd('

a bold b

')).to.equal('a **bold** b'); }); it('should not nest a mark inside the same mark', () => { expect(htmlToRemarkd('

a b

')).to.equal('**a b**'); }); it('should drop links that are not web, mail or phone links', () => { expect(htmlToRemarkd('

x y z

')).to.equal('x y **z**'); }); it('should keep code blocks verbatim', () => { expect(htmlToRemarkd('
  indented\n- not a list\n
')) .to.equal('```\n indented\n- not a list\n```'); }); it('should keep quotes, rules and tables', () => { expect(htmlToRemarkd('

quoted


')) .to.equal('____\nquoted\n____\n\n---'); expect(htmlToRemarkd('
NameValue
Alpha1
')) .to.equal('|===\n|Name |Value\n\n|Alpha |**1**\n|==='); }); it('should read Google Docs styling', () => { const html = '' + '

Bold and ' + 'italic

' + '
'; expect(htmlToRemarkd(html)).to.equal('**Bold** and __italic__\n\n- one\n-- nested\n- two'); }); it('should read Word list paragraphs', () => { const html = '

' + '1.  First

' + '

' + 'a. Sub

' + '

' + '2. Second

' + '

After

' + '

' + '· Bullet

'; expect(htmlToRemarkd(html)).to.equal('1. First\n1.1. Sub\n2. Second\n\nAfter\n\n- Bullet'); }); it('should produce source remarkd renders as the same structure', () => { const source = htmlToRemarkd('
  1. a
    1. b
  2. c
')!; expect(parse(source)).to.contain('
  1. a

    \n
    1. b

  2. \n
  3. c

'); }); it('should leave HTML with no formatting to the plain-text paste', () => { expect(htmlToRemarkd('
const x = 1;
')).to.be.null; expect(htmlToRemarkd('

just text

')).to.be.null; }); }); describe('plainTextToRemarkd', () => { it('should turn bullet glyphs into list markers, nesting by indent', () => { expect(plainTextToRemarkd('Intro\n• one\n ◦ nested\n• two')).to.equal('Intro\n- one\n-- nested\n- two'); }); it('should leave text without bullet glyphs alone', () => { expect(plainTextToRemarkd('- already remarkd')).to.be.null; }); }); describe('pastedRemarkd', () => { it('should prefer the HTML flavour', () => { expect(pastedRemarkd(clipboard({'text/html': '', 'text/plain': 'a'}))).to.equal('- a'); }); it('should leave code editor copies to the plain-text paste', () => { expect(pastedRemarkd(clipboard({ 'text/html': '
**bold**
', 'text/plain': '**bold**', 'vscode-editor-data': '{}', }))).to.be.null; }); });