From 13cf92ffff9c61f4ccd58900a2f441c801360a97 Mon Sep 17 00:00:00 2001 From: bendtherules Date: Sat, 21 Dec 2019 15:16:48 +0530 Subject: [PATCH] tagHandler - Handle list and listitems --- __tests__/renderer-spec.tsx | 24 ++++++++++++++++++++++++ src/tagHandler.ts | 15 ++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/__tests__/renderer-spec.tsx b/__tests__/renderer-spec.tsx index ed6096f..6fc266c 100644 --- a/__tests__/renderer-spec.tsx +++ b/__tests__/renderer-spec.tsx @@ -75,3 +75,27 @@ test('should render H1 with mixed strong and em text', () => { ) as string).trim() ).toBe('# This is **Hello** _World_'); }); + +test('should render plain unordered list with text', () => { + expect( + (render( + + ) as string).trim() + ).toBe('- a\n- b\n- c'); +}); + +test('should render plain ordered list with text', () => { + expect( + (render( +
    +
  1. a
  2. +
  3. b
  4. +
  5. c
  6. +
+ ) as string).trim() + ).toBe('1. a\n2. b\n3. c'); +}); diff --git a/src/tagHandler.ts b/src/tagHandler.ts index 85334ed..07a0e07 100644 --- a/src/tagHandler.ts +++ b/src/tagHandler.ts @@ -1,4 +1,4 @@ -import { emphasis, heading, strong } from 'mdast-builder'; +import { emphasis, heading, strong, list, listItem } from 'mdast-builder'; // tslint:disable-next-line: no-implicit-dependencies import { Node as MDASTNode, Parent as MDASTParent } from 'unist'; import { IReactElementSubset } from './helpers'; @@ -40,6 +40,19 @@ export default function handleTag( break; // END - Handle strong and em + // START - Handle lists - ul, ol, li + case 'ul': + newParentASTNode = childASTNode = list('unordered', []); + break; + case 'ol': + newParentASTNode = childASTNode = list('ordered', []); + break; + case 'li': + newParentASTNode = childASTNode = listItem([]); + break; + + // END - Handle lists - ul, ol, li + default: newParentASTNode = parentASTNode; break;