Changes
9 changed files (+715/-4)
-
src/combinator.ts (new)
-
@@ -0,0 +1,62 @@import { Node } from '~/types/Node' import { Parser, ParseText } from '~/types/Parser' export const or = (parsers: Parser[]): Parser => { const { length } = parsers return (text, position, rootParser) => { for (let i = 0; i < length; i++) { const match = parsers[i](text, position, rootParser) if (match) { return match } } return null } } export const regexp = ( pattern: RegExp, callback: ( match: string[], text: string, position: number, parseText: ParseText ) => [Node, number] | null ): Parser => (text, position, parseText) => { const match = text.substring(position).match(pattern) if (!match) { return null } return callback(match, text, position, parseText) } export const explicit = (parser: Parser): Parser => ( text, position, parseText ) => { const prevChar = text.charAt(position - 1) if (prevChar && !prevChar.match(/\s/)) { return null } return parser(text, position, parseText) } export const topOfLine = (parser: Parser): Parser => ( text, position, parseText ) => { if (position > 0 && text.charAt(position - 1) !== '\n') { return null } return parser(text, position, parseText) }
-
-
src/index.spec.ts (deleted)
-
@@ -1,3 +0,0 @@it('Should pass', () => { expect(1).toBe(1) })
-
-
-
@@ -1,1 +1,54 @@console.log('Hello, World!') import { Node, NodeType } from '~/types/Node' import { ParseText } from '~/types/Parser' import parser from '~/parser' const parseText: ParseText = text => { const children: Node[] = [] let textBuffer = '' const flush = () => { if (!textBuffer) { return } children.push({ type: NodeType.Text, text: textBuffer }) textBuffer = '' } let i = 0 const l = text.length while (i < l) { const match = parser(text, i, parseText) if (match) { flush() const [node, position] = match children.push(node) i = position continue } textBuffer += text.charAt(i) i += 1 } flush() return children } export const parse = (message: string): Node => { return { type: NodeType.Root, children: parseText(message) } } export default parse
-
-
src/parser.ts (new)
-
@@ -0,0 +1,198 @@import { NodeType } from '~/types/Node' import { explicit, or, regexp, topOfLine } from '~/combinator' const parseBold = explicit( regexp( /^\*(\S([^*\n]*?|[^*\n]*? `.*?` )\S|\S)\*(?=\s|$)/, (match, text, position, parseText) => { const [matchedText, content] = match return [ { type: NodeType.Bold, children: parseText(content) }, position + matchedText.length ] } ) ) const parseCode = explicit( regexp(/^`([^`]+?)`(?=\s|$)/, (match, text, position) => { const [matchedText, content] = match return [ { type: NodeType.Code, text: content }, position + matchedText.length ] }) ) const parsePreText = explicit( regexp(/^```(\s*\S[\s\S]*?\s*)```(?=\s|$)/, (match, text, position) => { const [matchedText, content] = match return [ { type: NodeType.PreText, text: content }, position + matchedText.length ] }) ) const parseItalic = explicit( regexp( /^_(\S([^_\n]*?|[^_\n]*? `.*?` )\S|\S)\_(?=\s|$)/, (match, text, position, parseText) => { const [matchedText, content] = match return [ { type: NodeType.Italic, children: parseText(content) }, position + matchedText.length ] } ) ) const parseStrike = explicit( regexp( /^~(\S([^~\n]*?|[^~\n]*? `.*?` )\S|\S)\~(?=\s|$)/, (match, text, position, parseText) => { const [matchedText, content] = match return [ { type: NodeType.Strike, children: parseText(content) }, position + matchedText.length ] } ) ) const parseSingleLineQuote = topOfLine( regexp(/^>(.*)(\n|$)/, (match, text, position, parseText) => { const [matchedText, content] = match const repeatedGt = content.match(/^((>)+)(.*)$/) return [ { type: NodeType.Quote, children: repeatedGt ? [ { type: NodeType.Text, text: repeatedGt[1] }, ...parseText(repeatedGt[3]) ] : parseText(content) }, position + matchedText.length ] }) ) const parseMultilineQuote = topOfLine( regexp(/^>>>([\s\S]+)$/, (match, text, position, parseText) => { const [matchedText, content] = match return [ { type: NodeType.Quote, children: parseText(content) }, position + matchedText.length ] }) ) const parseEmoji = explicit( regexp(/^:([^:]+?)(::([^:]+?))?:(?=\s|$)/, (match, text, position) => { const [matchedText, name, _, variation] = match return [ { type: NodeType.Emoji, name, variation }, position + matchedText.length ] }) ) const parseLink = regexp( /^<([^\s*~_<>]+?)(\|([^<>]+?))?>/, (match, text, position, parseText) => { const [matchedText, link, _, label] = match const nextPosition = position + matchedText.length const labelNodes = label ? parseText(label) : undefined switch (link.charAt(0)) { case '@': return [ { type: NodeType.UserLink, userID: link.slice(1), label: labelNodes }, nextPosition ] case '#': return [ { type: NodeType.ChannelLink, channelID: link.slice(1), label: labelNodes }, nextPosition ] case '!': const [commandName, ...args] = link.slice(1).split('^') return [ { type: NodeType.Command, name: commandName, arguments: args, label: labelNodes }, nextPosition ] default: return [ { type: NodeType.URL, url: link, label: labelNodes }, nextPosition ] } } ) export default or([ parseBold, parsePreText, parseCode, parseEmoji, parseItalic, parseMultilineQuote, parseSingleLineQuote, parseLink, parseStrike ])
-
-
src/types/Node.ts (new)
-
@@ -0,0 +1,105 @@export enum NodeType { Text, ChannelLink, UserLink, URL, Command, Emoji, PreText, Code, Italic, Bold, Strike, Quote, Root } export type Node = | Text | ChannelLink | UserLink | URL | Command | Emoji | PreText | Code | Italic | Bold | Strike | Quote | Root interface NodeBase { type: NodeType } export interface Text extends NodeBase { type: NodeType.Text text: string } export interface ChannelLink extends NodeBase { type: NodeType.ChannelLink channelID: string label?: Node[] } export interface UserLink extends NodeBase { type: NodeType.UserLink userID: string label?: Node[] } export interface URL extends NodeBase { type: NodeType.URL url: string label?: Node[] } export interface Command extends NodeBase { type: NodeType.Command name: string arguments: string[] label?: Node[] } export interface Emoji extends NodeBase { type: NodeType.Emoji name: string variation?: string } export interface PreText extends NodeBase { type: NodeType.PreText text: string } export interface Code extends NodeBase { type: NodeType.Code text: string } export interface Italic extends NodeBase { type: NodeType.Italic children: Node[] } export interface Bold extends NodeBase { type: NodeType.Bold children: Node[] } export interface Strike extends NodeBase { type: NodeType.Strike children: Node[] } export interface Quote extends NodeBase { type: NodeType.Quote children: Node[] } export interface Root extends NodeBase { type: NodeType.Root children: Node[] }
-
-
src/types/Parser.ts (new)
-
@@ -0,0 +1,9 @@import { Node } from '~/types/Node' export type Parser = ( text: string, position: number, parseText: ParseText ) => [Node, number] | null export type ParseText = (text: string) => Node[]
-
-
tests/helpers.ts (new)
-
@@ -0,0 +1,76 @@import { Node, NodeType } from '~/types/Node' export const root = (children: Node[]): Node => ({ type: NodeType.Root, children }) export const text = (t: string): Node => ({ type: NodeType.Text, text: t }) export const strike = (children: Node[]): Node => ({ type: NodeType.Strike, children }) export const italic = (children: Node[]): Node => ({ type: NodeType.Italic, children }) export const bold = (children: Node[]): Node => ({ type: NodeType.Bold, children }) export const code = (text: string): Node => ({ type: NodeType.Code, text }) export const pre = (text: string): Node => ({ type: NodeType.PreText, text }) export const user = (userID: string, label?: Node[]): Node => ({ type: NodeType.UserLink, userID, label }) export const channel = (channelID: string, label?: Node[]): Node => ({ type: NodeType.ChannelLink, channelID, label }) export const command = ( name: string, args: string[], label?: Node[] ): Node => ({ type: NodeType.Command, name, arguments: args, label }) export const url = (link: string, label?: Node[]): Node => ({ type: NodeType.URL, url: link, label }) export const emoji = (name: string, variation?: string): Node => ({ type: NodeType.Emoji, name, variation }) export const quote = (children: Node[]): Node => ({ type: NodeType.Quote, children })
-
-
tests/index.spec.ts (new)
-
@@ -0,0 +1,207 @@import { parse } from '~/index' import { bold, channel, code, command, emoji, italic, pre, quote, root, strike, text, url, user } from './helpers' describe('Text parser', () => { it('Should parse "foo bar" as text', () => { expect(parse('foo bar')).toEqual(root([text('foo bar')])) }) }) describe('Code parser', () => { it('Should parse "`foo`" as code', () => { expect(parse('`foo`')).toEqual(root([code('foo')])) }) it('Should parse "` foo `" as code', () => { expect(parse('` foo `')).toEqual(root([code(' foo ')])) }) }) describe('PreText parser', () => { it('Should parse "```foo```" as pretext', () => { expect(parse('```foo```')).toEqual(root([pre('foo')])) }) it('Should parse "``` foo ```" as pretext', () => { expect(parse('``` foo ```')).toEqual(root([pre(' foo ')])) }) it('Should parse "``` foo ``` bar ``` baz ```" as two pretexts', () => { expect(parse('``` foo ``` bar ``` baz ```')).toEqual( root([pre(' foo '), text(' bar '), pre(' baz ')]) ) }) it('Should not parse "``` ```" as pretext', () => { expect(parse('``` ```')).toEqual(root([text('``` ```')])) }) it('Should not parse "foo```bar```baz" as pretext', () => { expect(parse('foo```bar```baz')).toEqual(root([text('foo```bar```baz')])) }) }) describe('Bold parser', () => { it('Should parse "*foo*" as bold', () => { expect(parse('*foo*')).toEqual(root([bold([text('foo')])])) }) it('Should parse "foo *bar* baz" as bold', () => { expect(parse('foo *bar* baz')).toEqual( root([text('foo '), bold([text('bar')]), text(' baz')]) ) }) it('Should not parse "foo*bar*baz" as bold', () => { expect(parse('foo*bar*baz')).toEqual(root([text('foo*bar*baz')])) }) it('Should not parse "*foo*bar" as bold', () => { expect(parse('*foo*bar')).toEqual(root([text('*foo*bar')])) }) it('Should not parse "foo * bar * baz" as bold', () => { expect(parse('foo * bar * baz')).toEqual(root([text('foo * bar * baz')])) }) }) describe('Link parser', () => { it('Should parse "Hey, <@FOO>!" as user link', () => { expect(parse('Hey, <@FOO>!')).toEqual( root([text('Hey, '), user('FOO'), text('!')]) ) }) it('Should parse "Goto <#FOO>?" as channel link', () => { expect(parse('Goto <#FOO>?')).toEqual( root([text('Goto '), channel('FOO'), text('?')]) ) }) it('Should parse "foo <!bar> baz" as command', () => { expect(parse('foo <!bar> baz')).toEqual( root([text('foo '), command('bar', []), text(' baz')]) ) }) it('Should parse command arguments', () => { expect(parse('<!foo^bar>')).toEqual(root([command('foo', ['bar'])])) }) it('Should parse "Visit <http://foo.bar> or email to <mailto:foo@bar>" as url', () => { const expected = root([ text('Visit '), url('http://foo.bar'), text(' or email to '), url('mailto:foo@bar') ]) expect( parse('Visit <http://foo.bar> or email to <mailto:foo@bar>') ).toEqual(expected) }) it('Should not allow nested link', () => { expect(parse('<http://foo|<http://bar>>')).toEqual( root([text('<http://foo|'), url('http://bar'), text('>')]) ) }) it('Should parse label', () => { expect(parse('<http://foo|bar>')).toEqual( root([url('http://foo', [text('bar')])]) ) }) it('Should allow formated text in label', () => { const expected = root([ url('http://foo', [bold([text('bar '), strike([text('baz')])])]) ]) expect(parse('<http://foo|*bar ~baz~*>')).toEqual(expected) }) }) describe('Emoji parser', () => { it('Should parse "foo :bar: baz" as emoji', () => { expect(parse('foo :bar: baz')).toEqual( root([text('foo '), emoji('bar'), text(' baz')]) ) }) it('Should parse emoji with variation', () => { expect(parse(':foo::bar:')).toEqual(root([emoji('foo', 'bar')])) }) it('Should not parse "foo:bar:baz" as emoji', () => { expect(parse('foo:bar:baz')).toEqual(root([text('foo:bar:baz')])) }) }) describe('Quote parser', () => { it('Should parse quote text', () => { expect(parse('> foo *bar*')).toEqual( root([quote([text(' foo '), bold([text('bar')])])]) ) }) it('Should parse quote locate in second line', () => { expect(parse('foo\n>bar')).toEqual( root([text('foo\n'), quote([text('bar')])]) ) }) it('Should parse multiline quote', () => { expect(parse('foo\n>>>bar\n\nbaz')).toEqual( root([text('foo\n'), quote([text('bar\n\nbaz')])]) ) }) it('Should not parse "foo>bar" as quote', () => { expect(parse('foo>bar')).toEqual(root([text('foo>bar')])) }) it('Should parse ">>>" as quoted ">>"', () => { expect(parse('>>>')).toEqual(root([quote([text('>>')])])) }) }) describe('Root parser', () => { it('Should parse slack message', () => { const expected = root([ user('FOO', [ text('abc '), strike([ text('def '), italic([ text('ghi '), bold([text('jkl '), emoji('+1')]), text(' mno') ]), text(' pqr') ]), text(' stu '), pre('vwx') ]) ]) expect( parse('<@FOO|abc ~def _ghi *jkl :+1:* mno_ pqr~ stu ```vwx```>') ).toEqual(expected) }) })
-
-
tests/tsconfig.json (new)
-
@@ -0,0 +1,4 @@{ "extends": "../tsconfig.json", "include": ["../src/**/*", "./**/*"] }
-