Changes
7 changed files (+85/-39)
-
-
@@ -32,7 +32,8 @@ // type: NodeType.Root,// children: [ // { // type: NodeType.Text, // text: "Slack " // text: "Slack ", // . source: "Slack " // }, // { // type: NodeType.Bold,
-
@@ -41,10 +42,12 @@ // {// type: NodeType.Text, // text: "message" // } // ] // ], // . source: "*message*" // }, // ... // ] // ], // source: "Slack *message* ~to~ _parse_" // } // Write your own!
-
@@ -61,7 +64,8 @@ return `<i>${node.children.map(toHTML).join('')}</i>`case NodeType.Strike: return `<del>${node.children.map(toHTML).join('')}</del>` default: return '' // You can use `source` property, which every nodes have, to serialize unknown nodes as-is return node.source } }
-
-
-
@@ -24,9 +24,10 @@ Represents each parts of the message, its type and properties.Every node at least have one property, `type`. | Name | Type | Description | | ---- | ---------------- | ---------------- | | type | Number(NodeType) | Type of the node | | Name | Type | Description | | ------ | ---------------- | ---------------------- | | type | Number(NodeType) | Type of the node | | source | String | Raw string of the node | You can test the type with `NodeType` object (which is actually TypeScript enum).
-
-
-
@@ -14,7 +14,8 @@ }children.push({ type: NodeType.Text, text: textBuffer text: textBuffer, source: textBuffer }) textBuffer = ''
-
@@ -47,7 +48,8 @@export const parse = (message: string): Node => { return { type: NodeType.Root, children: parseText(message) children: parseText(message), source: message } }
-
-
-
@@ -11,7 +11,8 @@return [ { type: NodeType.Bold, children: parseText(content) children: parseText(content), source: matchedText }, position + matchedText.length ]
-
@@ -26,7 +27,8 @@return [ { type: NodeType.Code, text: content text: content, source: matchedText }, position + matchedText.length ]
-
@@ -42,7 +44,8 @@return [ { type: NodeType.PreText, text: content text: content, source: matchedText }, position + matchedText.length ]
-
@@ -59,7 +62,8 @@return [ { type: NodeType.Italic, children: parseText(content) children: parseText(content), source: matchedText }, position + matchedText.length ]
-
@@ -76,7 +80,8 @@return [ { type: NodeType.Strike, children: parseText(content) children: parseText(content), source: matchedText }, position + matchedText.length ]
-
@@ -97,11 +102,13 @@ children: repeatedGt? [ { type: NodeType.Text, text: repeatedGt[1] text: repeatedGt[1], source: repeatedGt[1] }, ...parseText(repeatedGt[3]) ] : parseText(content) : parseText(content), source: matchedText }, position + matchedText.length ]
-
@@ -115,7 +122,8 @@return [ { type: NodeType.Quote, children: parseText(content) children: parseText(content), source: matchedText }, position + matchedText.length ]
-
@@ -131,7 +139,8 @@ return [{ type: NodeType.Emoji, name, variation variation, source: matchedText }, position + matchedText.length ]
-
@@ -151,7 +160,8 @@ return [{ type: NodeType.UserLink, userID: link.slice(1), label: labelNodes label: labelNodes, source: matchedText }, nextPosition ]
-
@@ -160,7 +170,8 @@ return [{ type: NodeType.ChannelLink, channelID: link.slice(1), label: labelNodes label: labelNodes, source: matchedText }, nextPosition ]
-
@@ -172,7 +183,8 @@ {type: NodeType.Command, name: commandName, arguments: args, label: labelNodes label: labelNodes, source: matchedText }, nextPosition ]
-
@@ -181,7 +193,8 @@ return [{ type: NodeType.URL, url: link, label: labelNodes label: labelNodes, source: matchedText }, nextPosition ]
-
-
-
@@ -31,6 +31,11 @@ | Rootinterface NodeBase { type: NodeType /** * Raw node text. */ source: string } export interface Text extends NodeBase {
-
-
-
@@ -1,50 +1,63 @@import { Node, NodeType } from '../src/types/Node' function source(children: Node[]): string { return children.map(c => c.source).join('') } export const root = (children: Node[]): Node => ({ type: NodeType.Root, children children, source: source(children) }) export const text = (t: string): Node => ({ type: NodeType.Text, text: t text: t, source: t }) export const strike = (children: Node[]): Node => ({ type: NodeType.Strike, children children, source: `~${source(children)}~` }) export const italic = (children: Node[]): Node => ({ type: NodeType.Italic, children children, source: `_${source(children)}_` }) export const bold = (children: Node[]): Node => ({ type: NodeType.Bold, children children, source: `*${source(children)}*` }) export const code = (text: string): Node => ({ type: NodeType.Code, text text, source: '`' + text + '`' }) export const pre = (text: string): Node => ({ type: NodeType.PreText, text text, source: '```' + text + '```' }) export const user = (userID: string, label?: Node[]): Node => ({ type: NodeType.UserLink, userID, label label, source: `<@${userID}${label ? '|' + source(label) : ''}>` }) export const channel = (channelID: string, label?: Node[]): Node => ({ type: NodeType.ChannelLink, channelID, label label, source: `<#${channelID}${label ? '|' + source(label) : ''}>` }) export const command = (
-
@@ -55,22 +68,28 @@ ): Node => ({type: NodeType.Command, name, arguments: args, label label, source: `<!${name}${args.map(c => `^${c}`).join('')}${ label ? '|' + source(label) : '' }>` }) export const url = (link: string, label?: Node[]): Node => ({ type: NodeType.URL, url: link, label label, source: `<${link}${label ? '|' + source(label) : ''}>` }) export const emoji = (name: string, variation?: string): Node => ({ type: NodeType.Emoji, name, variation variation, source: `:${name}${variation ? '::' + variation : ''}:` }) export const quote = (children: Node[]): Node => ({ export const quote = (children: Node[], inline?: boolean): Node => ({ type: NodeType.Quote, children children, source: `${'>'.repeat(inline ? 1 : 3)}${source(children)}` })
-
-
-
@@ -187,13 +187,13 @@describe('Quote parser', () => { it('Should parse quote text', () => { expect(parse('> foo *bar*')).toEqual( root([quote([text(' foo '), bold([text('bar')])])]) root([quote([text(' foo '), bold([text('bar')])], true)]) ) }) it('Should parse quote locate in second line', () => { expect(parse('foo\n>bar')).toEqual( root([text('foo\n'), quote([text('bar')])]) root([text('foo\n'), quote([text('bar')], true)]) ) })
-
@@ -208,7 +208,9 @@ expect(parse('foo>bar')).toEqual(root([text('foo>bar')]))}) it('Should parse ">>>" as quoted ">>"', () => { expect(parse('>>>')).toEqual(root([quote([text('>>')])])) expect(parse('>>>')).toEqual( root([quote([text('>>')], true)]) ) }) })
-