diff --git a/.gitignore b/.gitignore index 66c8416..5313f0c 100644 --- a/.gitignore +++ b/.gitignore @@ -60,5 +60,5 @@ typings/ # next.js build output .next -# Test folder -tests/ \ No newline at end of file +# Internal dir +internal/ \ No newline at end of file diff --git a/.npmignore b/.npmignore index cb0ac2a..975eb5e 100644 --- a/.npmignore +++ b/.npmignore @@ -1,2 +1,2 @@ -# Test dir -tests/ \ No newline at end of file +# Internal dir +internal/ \ No newline at end of file diff --git a/LICENSE b/LICENSE index e30f4be..792124b 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2019 e6Hub +Copyright (c) 2020 e6Hub Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 3f52fef..1e4f8d9 100644 --- a/README.md +++ b/README.md @@ -1,38 +1,45 @@ -> :warning: WIP +# DText parser +DText is the e621's markdown system, it's based on many languages like BBCode, MediaWiki, Textile, etc. This module parses DText to HTML. -# :u6e80: DText parser -DText is the e621's markdown system, it's based on many languages like BBCode, MediaWiki, Textile, etc. - -## :package: Install +## ๐Ÿ“ฆ Install ``` -yarn add dtext-parser +npm i -S dtext-parser ``` or ``` -npm install dtext-parser -s +yarn add dtext-parser ``` -## :wrench: Usage -### Quick start +## ๐Ÿงช Usage +### โšก Quick start ```js -import DText from 'dtext-parser' +const DText = require('dtext-parser'); DText.parse('h1.DText header') -.then(console.log); //

DText header

+ .then(console.log); //

DText header

``` -### Config +### โš™ Config Follow the name of the modules (codes) e.g. Bold, Italics, Code, External link, etc. then set your own class or style. ```js -import DText from 'dtext-parser' +const DText = require('dtext-parser'); DText.options({ "External link": { - style: "color:red; font-size:14pt", - class: "external-link" + attrs: [ + { + name: 'style', + value: 'color: red;' + }, + { + name: 'class', + value: 'extern' + } + ] } + }); }); DText.parse('https://github.com').then(console.log); -// https://github.com +// https://github.com ``` diff --git a/__tests__/parserFormats.test.js b/__tests__/parserFormats.test.js new file mode 100644 index 0000000..284341f --- /dev/null +++ b/__tests__/parserFormats.test.js @@ -0,0 +1,161 @@ +const dtext = require('../'); + +test('parse format Bold', () => { + return dtext.parse('[b]Test[/b]') + .then((result) => { + expect(result).toBe + ('Test'); + }); +}); + +test('parse format Italics', () => { + return dtext.parse('[i]Test[/i]') + .then((result) => { + expect(result).toBe + ('Test'); + }); +}); + +test('parse format Underline', () => { + return dtext.parse('[u]Test[/u]') + .then((result) => { + expect(result).toBe + ('Test'); + }); +}); + +test('parse format Overline', () => { + return dtext.parse('[o]Test[/o]') + .then((result) => { + expect(result).toBe + ('Test'); + }); +}); + +test('parse format Strikeout', () => { + return dtext.parse('[s]Test[/s]') + .then((result) => { + expect(result).toBe + ('Test'); + }); +}); + +test('parse format Superscript', () => { + return dtext.parse('[sup]Test[/sup]') + .then((result) => { + expect(result).toBe + ('Test'); + }); +}); + +test('parse format Subscript', () => { + return dtext.parse('[sub]Test[/sub]') + .then((result) => { + expect(result).toBe + ('Test'); + }); +}); + +test('parse format Username', () => { + return dtext.parse('@Test') + .then((result) => { + expect(result).toBe + ('@Test'); + }); +}); + +test('parse format Spoiler', () => { + return dtext.parse('[spoiler]Test[/spoiler]') + .then((result) => { + expect(result).toBe + ('Test'); + }); +}); + +test('parse format Code', () => { + return dtext.parse('`Test`') + .then((result) => { + expect(result).toBe + ('Test'); + }); +}); + +test('parse format Inline code', () => { + return dtext.parse('`Test`') + .then((result) => { + expect(result).toBe + ('Test'); + }); +}); + +test('parse format Color', () => { + return dtext.parse('[color=#f00]Test[/color]') + .then((result) => { + expect(result).toBe + ('Test'); + }); +}); + +test('parse format External link', () => { + return dtext.parse('https://google.com') + .then((result) => { + expect(result).toBe + ('https://google.com'); + }); +}); + +test('parse format Wiki link', () => { + return dtext.parse('[[test]]') + .then((result) => { + expect(result).toBe + ('test'); + }); +}); + +test('parse format Tag link', () => { + return dtext.parse('{{test}}') + .then((result) => { + expect(result).toBe + ('test'); + }); +}); + +test('parse format Intern link', () => { + return dtext.parse('post #1234') + .then((result) => { + expect(result).toBe + ('post #1234'); + }); +}); + +test('parse format Quote', () => { + return dtext.parse('[quote]Test[/quote]') + .then((result) => { + expect(result).toBe + ('
Test
'); + }); +}); + +test('parse format Code', () => { + return dtext.parse('[code]Test[/code]') + .then((result) => { + expect(result).toBe + ('
Test
'); + }); +}); + +test('parse format Header', () => { + return dtext.parse('h1.Test') + .then((result) => { + expect(result).toBe + ('

Test

'); + }); +}); + +test('parse format Section', () => { + return dtext.parse('[section,expanded="Test"]Test[/section]') + .then((result) => { + expect(result).toBe + ('