├── .babelrc ├── .eslintrc ├── .gitignore ├── .travis.yml ├── Gruntfile.js ├── LICENSE ├── README.md ├── index.js ├── package.json ├── src └── index.js └── test └── index.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | "es2015" 4 | ], 5 | "plugins": ["add-module-exports"] 6 | } 7 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "parser": "babel-eslint", 3 | "extends": "airbnb/base", 4 | "rules": { 5 | "comma-dangle": [2, "never"] 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | #### joe made this: https://goel.io/joe 2 | 3 | #####=== Node ===##### 4 | 5 | # Logs 6 | logs 7 | *.log 8 | 9 | # Runtime data 10 | pids 11 | *.pid 12 | *.seed 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directory 30 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- 31 | node_modules 32 | 33 | # Debug log from npm 34 | npm-debug.log 35 | 36 | dist 37 | 38 | .DS_Store 39 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "4" 4 | - "5" 5 | - "6" 6 | -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | module.exports = grunt => { 2 | require('load-grunt-tasks')(grunt); 3 | 4 | grunt.initConfig({ 5 | eslint: { 6 | files: { 7 | expand: true, 8 | src: 'src/**/*.js' 9 | } 10 | }, 11 | babel: { 12 | options: { 13 | sourceMap: true, 14 | presets: ['es2015', 'stage-0'], 15 | plugins: ['add-module-exports'] 16 | }, 17 | files: { 18 | expand: true, 19 | cwd: 'src', 20 | src: '**/*.js', 21 | dest: 'dist' 22 | } 23 | }, 24 | watch: { 25 | scripts: { 26 | files: ['src/**/*.js'], 27 | tasks: ['lint', 'babel'], 28 | options: { 29 | spawn: false 30 | } 31 | } 32 | } 33 | }); 34 | 35 | grunt.registerTask('lint', ['eslint']); 36 | grunt.registerTask('default', ['watch']); 37 | }; 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Mohamad Jahani 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/mamal72/rtl-md.svg?branch=master)](https://travis-ci.org/mamal72/rtl-md) 2 | [![npm version](https://badge.fury.io/js/rtl-md.svg)](https://badge.fury.io/js/rtl-md) 3 | # rtl-md 4 | **rtl-md** is a markdown parser with RTL languages detection. This module converts the `Markdown` to `HTML` and fixes the elements direction based on the contents of them. This module made in order to help users publish their RTL markdown contents easily, without having to write boring HTML. Now, I can write and publish my `README.md` files in RTL languages too just like LTR languages! 5 | 6 | 7 | # Installation 8 | ```bash 9 | npm install --save rtl-md 10 | ``` 11 | 12 | # Usage 13 | ```js 14 | import rmd from 'rtl-md'; 15 | const md = `# Some Markdown! 16 | ## Some english ltr stuff here and then, some rtl maybe. 17 | 18 | بنی آدم اعضای یکدیگرند 19 | 20 | که در آفرینش ز یک گوهرند 21 | 22 | [سعدی شیرازی](https://en.wikiquote.org/wiki/Saadi) 23 | ` 24 | const html = rmd(md); 25 | 26 | // The output 27 | /* 28 |

Some Markdown!

29 |

Some english ltr stuff here and then, some rtl maybe.

30 |

بنی آدم اعضای یکدیگرند

31 |

که در آفرینش ز یک گوهرند

32 |

سعدی شیرازی

33 | */ 34 | ``` 35 | 36 | 37 | # Ideas || Issues 38 | Just fill an issue and describe it. I'll check it ASAP! 39 | 40 | 41 | # Contribution 42 | You can fork the repository, improve or fix some part of it and then send the pull requests back if you want to see them here. I really appreciate that. :heart: 43 | 44 | Remember to lint your code before sending pull requests. Run the Grunt lint task with the following command and fix the lint errors if you get any. 45 | ```bash 46 | grunt lint 47 | ``` 48 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./dist'); 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rtl-md", 3 | "version": "1.0.1", 4 | "description": "A simple markdown to html converter which knows the direction!", 5 | "main": "index.js", 6 | "scripts": { 7 | "build": "babel src --out-dir dist", 8 | "lint": "eslint src test", 9 | "test": "npm run build && mocha --compilers js:babel-core/register test/index.js" 10 | }, 11 | "files": [ 12 | "dist" 13 | ], 14 | "repository": { 15 | "type": "git", 16 | "url": "git+https://github.com/mamal72/rtl-md.git" 17 | }, 18 | "keywords": [ 19 | "markdown", 20 | "rtl", 21 | "html", 22 | "readme" 23 | ], 24 | "author": "Mohamad Jahani (https://github.com/mamal72)", 25 | "license": "MIT", 26 | "bugs": { 27 | "url": "https://github.com/mamal72/rtl-md/issues" 28 | }, 29 | "homepage": "https://github.com/mamal72/rtl-md#readme", 30 | "dependencies": { 31 | "cheerio": "^0.20.0", 32 | "html-entities": "^1.2.0", 33 | "marked": "^0.3.5" 34 | }, 35 | "devDependencies": { 36 | "babel-cli": "^6.9.0", 37 | "babel-core": "^6.9.1", 38 | "babel-eslint": "^6.0.4", 39 | "babel-plugin-add-module-exports": "^0.2.1", 40 | "babel-preset-es2015": "^6.9.0", 41 | "babel-preset-stage-0": "^6.5.0", 42 | "chai": "^3.5.0", 43 | "eslint": "^2.11.1", 44 | "eslint-config-airbnb": "^9.0.1", 45 | "grunt": "^1.0.1", 46 | "grunt-babel": "^6.0.0", 47 | "grunt-contrib-watch": "^1.0.0", 48 | "grunt-eslint": "^18.1.0", 49 | "load-grunt-tasks": "^3.5.0", 50 | "mocha": "^2.5.3" 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import marked from 'marked'; 2 | import cheerio from 'cheerio'; 3 | import { AllHtmlEntities as HtmlEntities } from 'html-entities'; 4 | 5 | function isRTL(string) { 6 | const regexString = '[\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC]'; 7 | const rtlDirCheck = new RegExp(regexString); 8 | return rtlDirCheck.test(string); 9 | } 10 | 11 | export default function convert(markdownString, options = {}) { 12 | const ltrMarkup = marked(markdownString, options); 13 | const $ = cheerio.load(ltrMarkup); 14 | const elems = $('*'); 15 | elems.filter((index, elem) => { 16 | // Filter RTL containers with text content 17 | return elem.type === 'tag' && elem.children[0].type === 'text' && isRTL(elem.children[0].data); 18 | }).each((index, elem) => { 19 | // If there is no siblings, go for the parents 20 | if (elem.next === null && elem.prev === null) { 21 | cheerio(elem).parent().attr('dir', 'rtl'); 22 | } else { 23 | cheerio(elem).attr('dir', 'rtl'); 24 | } 25 | }); 26 | return HtmlEntities.decode($.html()); 27 | } 28 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | /* eslint-env mocha */ 2 | 3 | // ES6 Modules "import" wasn't working! :/ Dunno why! :'( 4 | const rtlMarkdown = require('../'); 5 | const chai = require('chai'); 6 | 7 | chai.should(); 8 | 9 | describe('RTL Markdown Tests:', () => { 10 | it('Should convert markdown to html', () => { 11 | const from = 'I am using __markdown__.'; 12 | const to = '

I am using markdown.

\n'; 13 | const converted = rtlMarkdown(from); 14 | converted.should.equal(to); 15 | }); 16 | 17 | it('Should convert markdown that contains html code', () => { 18 | const html = '

Some HTML data!

'; 19 | const converted = rtlMarkdown(html); 20 | converted.should.equal(html); 21 | }); 22 | 23 | it('Should understand RTL languages and fix their direction if needed', () => { 24 | // Markdown with a RTL stuff 25 | const from = `# Tanha Font 26 | A Persian (Farsi) Font 27 | 28 | فونت (قلم) فارسی تنها 29 | 30 | [نمایش فونت](http://rastikerdar.github.io/tanha-font/) 31 | 32 | با تشکر از برنامه [FontForge](https://fontforge.github.io) 33 | 34 | بر مبنای فونت [صمیم](http://rastikerdar.github.io/samim-font/) و [وزیر](http://rastikerdar.github.io/vazir-font/) 35 | 36 | 37 | طریقه استفاده در صفحات وب: 38 | -------------------------- 39 |
40 | کد زیر را در قسمت style یا فایل css وارد نمایید: 41 |
`; 42 | 43 | // RTLed HTML Markup 44 | const to = `

Tanha Font

45 |

A Persian (Farsi) Font

46 |

فونت (قلم) فارسی تنها

47 |

نمایش فونت

48 |

با تشکر از برنامه FontForge

49 |

بر مبنای فونت صمیم و وزیر

50 |

طریقه استفاده در صفحات وب:

51 |
52 | کد زیر را در قسمت style یا فایل css وارد نمایید: 53 |
`; 54 | 55 | const converted = rtlMarkdown(from); 56 | converted.should.equal(to); 57 | }); 58 | }); 59 | --------------------------------------------------------------------------------