Skip to content

Commit

Permalink
adding jest, parse is now per line
Browse files Browse the repository at this point in the history
  • Loading branch information
Saektide committed Nov 16, 2020
1 parent 3f6e599 commit eedd7ab
Show file tree
Hide file tree
Showing 12 changed files with 5,349 additions and 379 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,5 @@ typings/
# next.js build output
.next

# Test folder
tests/
# Internal dir
internal/
4 changes: 2 additions & 2 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Test dir
tests/
# Internal dir
internal/
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
39 changes: 23 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -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); // <h1>DText header</h1>
.then(console.log); // <h1>DText header</h1>
```

### 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);
// <a href="https://github.com" class="external-link" style="color:red; font-size:14pt">https://github.com</a>
// <a href="https://github.com" class="extern" style="color: red;">https://github.com</a>
```
161 changes: 161 additions & 0 deletions __tests__/parserFormats.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
const dtext = require('../');

test('parse format Bold', () => {
return dtext.parse('[b]Test[/b]')
.then((result) => {
expect(result).toBe
('<b>Test</b>');
});
});

test('parse format Italics', () => {
return dtext.parse('[i]Test[/i]')
.then((result) => {
expect(result).toBe
('<i>Test</i>');
});
});

test('parse format Underline', () => {
return dtext.parse('[u]Test[/u]')
.then((result) => {
expect(result).toBe
('<u>Test</u>');
});
});

test('parse format Overline', () => {
return dtext.parse('[o]Test[/o]')
.then((result) => {
expect(result).toBe
('<o>Test</o>');
});
});

test('parse format Strikeout', () => {
return dtext.parse('[s]Test[/s]')
.then((result) => {
expect(result).toBe
('<s>Test</s>');
});
});

test('parse format Superscript', () => {
return dtext.parse('[sup]Test[/sup]')
.then((result) => {
expect(result).toBe
('<sup>Test</sup>');
});
});

test('parse format Subscript', () => {
return dtext.parse('[sub]Test[/sub]')
.then((result) => {
expect(result).toBe
('<sub>Test</sub>');
});
});

test('parse format Username', () => {
return dtext.parse('@Test')
.then((result) => {
expect(result).toBe
('<b>@Test</b>');
});
});

test('parse format Spoiler', () => {
return dtext.parse('[spoiler]Test[/spoiler]')
.then((result) => {
expect(result).toBe
('<span class="spoiler">Test</span>');
});
});

test('parse format Code', () => {
return dtext.parse('`Test`')
.then((result) => {
expect(result).toBe
('<code>Test</code>');
});
});

test('parse format Inline code', () => {
return dtext.parse('`Test`')
.then((result) => {
expect(result).toBe
('<code>Test</code>');
});
});

test('parse format Color', () => {
return dtext.parse('[color=#f00]Test[/color]')
.then((result) => {
expect(result).toBe
('<span style="color:#f00">Test</span>');
});
});

test('parse format External link', () => {
return dtext.parse('https://google.com')
.then((result) => {
expect(result).toBe
('<a href="https://google.com">https://google.com</a>');
});
});

test('parse format Wiki link', () => {
return dtext.parse('[[test]]')
.then((result) => {
expect(result).toBe
('<a href="https://e621.net/wiki_pages/test">test</a>');
});
});

test('parse format Tag link', () => {
return dtext.parse('{{test}}')
.then((result) => {
expect(result).toBe
('<a href="https://e621.net/posts?tags=test">test</a>');
});
});

test('parse format Intern link', () => {
return dtext.parse('post #1234')
.then((result) => {
expect(result).toBe
('<a href="https://e621.net/posts/1234">post #1234</a>');
});
});

test('parse format Quote', () => {
return dtext.parse('[quote]Test[/quote]')
.then((result) => {
expect(result).toBe
('<blockquote>Test</blockquote>');
});
});

test('parse format Code', () => {
return dtext.parse('[code]Test[/code]')
.then((result) => {
expect(result).toBe
('<pre>Test</pre>');
});
});

test('parse format Header', () => {
return dtext.parse('h1.Test')
.then((result) => {
expect(result).toBe
('<h1>Test</h1>');
});
});

test('parse format Section', () => {
return dtext.parse('[section,expanded="Test"]Test[/section]')
.then((result) => {
expect(result).toBe
('<section class="section collapsed"><div class="section-collapse-title">"Test"]Test[/section</div><div class="section-collapsed-title">"Test"]Test[/section</div><div class="section-content">');
});
});
23 changes: 23 additions & 0 deletions __tests__/parserHtmlAttrs.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const dtext = require('../');

test('parse html attributes', () => {
dtext.options({
"External link": {
attrs: [
{
name: 'style',
value: 'color: red;'
},
{
name: 'class',
value: 'extern'
}
]
}
});

return dtext.parse('https://google.com').then((result) => {
expect(result).toBe
('<a href="https://google.com" style="color: red;" class="extern">https://google.com</a>');
});
});
25 changes: 25 additions & 0 deletions __tests__/parserScenarios.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const dtext = require('../');

test('parse scenario Simple article', () => {
return dtext.parse('h1.Hello\nHello world!')
.then((result) => {
expect(result).toBe
('<h1>Hello</h1><p>Hello world!</p>');
});
});

test('parse scenario Paragraphs', () => {
return dtext.parse('Hello foo!\nHello bar!')
.then((result) => {
expect(result).toBe
('<p>Hello foo!</p><p>Hello bar!</p>');
});
});

test('parse scenario Section', () => {
return dtext.parse('[section=Section title]\nh1.Some cool title\nRemake of post #1234[/section]')
.then((result) => {
expect(result).toBe
('<section class="section expanded"><div class="section-collapse-title">Section title</div><div class="section-collapsed-title">Section title</div><div class="section-content"><h1>Some cool title</h1><p>Remake of <a href="https://e621.net/posts/1234">post #1234</a></p>\n</div></section>');
});
});
Loading

0 comments on commit eedd7ab

Please sign in to comment.