{"id":24983651,"url":"https://github.com/posthtml/posthtml-rtl","last_synced_at":"2025-03-29T10:12:37.667Z","repository":{"id":57339121,"uuid":"382706827","full_name":"posthtml/posthtml-rtl","owner":"posthtml","description":"A flexible utility to convert HTML to RTL (right to left) and vice versa.","archived":false,"fork":false,"pushed_at":"2021-07-25T01:44:10.000Z","size":148,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-29T21:06:07.432Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/posthtml.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-07-03T20:47:43.000Z","updated_at":"2024-05-02T01:47:33.000Z","dependencies_parsed_at":"2022-08-28T10:00:49.435Z","dependency_job_id":null,"html_url":"https://github.com/posthtml/posthtml-rtl","commit_stats":null,"previous_names":["posthtml/posthtml-rtl","ahmed3hamdan/posthtml-rtl"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/posthtml%2Fposthtml-rtl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/posthtml%2Fposthtml-rtl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/posthtml%2Fposthtml-rtl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/posthtml%2Fposthtml-rtl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/posthtml","download_url":"https://codeload.github.com/posthtml/posthtml-rtl/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246100567,"owners_count":20723479,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-04T09:20:02.688Z","updated_at":"2025-03-29T10:12:37.640Z","avatar_url":"https://github.com/posthtml.png","language":"JavaScript","readme":"# posthtml-rtl\n\nA flexible utility to convert HTML to RTL (right to left) and vice versa.\n\n## Introduction\n\n`posthtml-rtl` converts the CSS embedded in HTML files such as internal style tags and inline style attributes using [rtlcss](https://github.com/MohammadYounes/rtlcss). Moreover, the attributes `align` and `dir` are mirrored. Furthermore, the package provide a way to ignore, remove or overwrite tags/attributes.\n\nAn example showing the input and output of `posthtml-rtl`:\n\n```html\n\u003c!-- Input --\u003e\n\u003chtml dir=\"ltr\" lang=\"en-US\" data-rtl-lang=\"ar-AE\"\u003e\n  \u003chead\u003e\n    \u003cstyle\u003e\n      .foo {\n        float: left;\n      }\n    \u003c/style\u003e\n  \u003c/head\u003e\n  \u003cbody\u003e\n    \u003ctable style=\"margin-left: 20px\"\u003e\n      \u003ctbody\u003e\n        \u003ctr\u003e\n          \u003ctd align=\"right\"\u003e1\u003c/td\u003e\n          \u003ctd align=\"left\"\u003e2\u003c/td\u003e\n          \u003ctd align=\"left\" data-rtl-ignore\u003e3\u003c/td\u003e\n        \u003c/tr\u003e\n      \u003c/tbody\u003e\n    \u003c/table\u003e\n  \u003c/body\u003e\n\u003c/html\u003e\n\n\u003c!-- RTL output --\u003e\n\u003chtml dir=\"rtl\" lang=\"ar-AE\"\u003e\n  \u003chead\u003e\n    \u003cstyle\u003e\n      .foo {\n        float: right;\n      }\n    \u003c/style\u003e\n  \u003c/head\u003e\n  \u003cbody\u003e\n    \u003ctable style=\"margin-right: 20px\"\u003e\n      \u003ctbody\u003e\n        \u003ctr\u003e\n          \u003ctd align=\"left\"\u003e1\u003c/td\u003e\n          \u003ctd align=\"right\"\u003e2\u003c/td\u003e\n          \u003ctd align=\"left\"\u003e3\u003c/td\u003e\n        \u003c/tr\u003e\n      \u003c/tbody\u003e\n    \u003c/table\u003e\n  \u003c/body\u003e\n\u003c/html\u003e\n\n\u003c!-- LTR output --\u003e\n\u003chtml dir=\"ltr\" lang=\"en-US\"\u003e\n  \u003chead\u003e\n    \u003cstyle\u003e\n      .foo {\n        float: left;\n      }\n    \u003c/style\u003e\n  \u003c/head\u003e\n  \u003cbody\u003e\n    \u003ctable style=\"margin-left: 20px\"\u003e\n      \u003ctbody\u003e\n        \u003ctr\u003e\n          \u003ctd align=\"right\"\u003e1\u003c/td\u003e\n          \u003ctd align=\"left\"\u003e2\u003c/td\u003e\n          \u003ctd align=\"left\"\u003e3\u003c/td\u003e\n        \u003c/tr\u003e\n      \u003c/tbody\u003e\n    \u003c/table\u003e\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\n## Use Cases\n\nThis tools is useful for the following cases:\n\n- Convert HTML email templates to RTL.\n- Convert static HTML website to RTL (in combination with [rtlcss](https://github.com/MohammadYounes/rtlcss) for external CSS files).\n- As a part of your website HTML generation; this package can be used in combination with template engine like `pug` and internationalization library like `i18next` to support RTL locales interface in multi-language websites.\n\n## Installation\n\nInstall as local dependency using `npm`:\n\n```bash\nnpm install --save posthtml-rtl\n```\n\nor using `yarn`:\n\n```bash\nyarn add posthtml-rtl\n```\n\nInstall as CLI utility:\n\n```bash\nnpm install --global posthtml-rtl\n```\n\n## Usage\n\n### CLI Utility\n\n```bash\nposthtml-rtl \u003cfile\u003e\n```\n\nFlags\n\n```\n  --help          Display help instructions\n  --rtl           Output RTL HTML (true by default)\n  -o, --output    Path to write output (optional)\n```\n\nBy default the CLI utility reads `stdin` as input and dump the output into `stdout`\n\n```bash\necho '\u003chtml dir=\"rtl\"\u003e\u003c/html\u003e' | posthtml-rtl\n```\n\nYou can provide input file as an argument\n\n```bash\nposthtml-rtl ./index.html\n```\n\nYou can also specify an output file with `output` flag:\n\n```bash\nposthtml-rtl ./index.html --output=./ar/index.html\n```\n\nThe `rtl` flag determine weather the output should be converted to RTL or not.\n\nFor example `index.html` contains\n\n```html\n\u003chtml dir=\"ltr\" lang=\"en-US\" data-rtl-lang=\"ar-AE\" data-ltr-remove=\"dir\"\u003e\u003c/html\u003e\n```\n\nWhen running the utility with `rtl` flag as `true`\n\n```bash\n  posthtml-rtl --rtl=true index.html\n```\n\nOutputs\n\n```html\n\u003chtml dir=\"rtl\" lang=\"ar-AE\"\u003e\u003c/html\u003e\n```\n\nAnd when `rtl` is `false`\n\n```bash\n  posthtml-rtl --rtl=false index.html\n```\n\nOutputs\n\n```html\n\u003chtml lang=\"en-US\"\u003e\u003c/html\u003e\n```\n\n### Node.js\n\n```js\nconst posthtmlRtl = require(\"posthtml-rtl\");\n\nconst inputHtml = `\u003chtml dir=\"ltr\"\u003e\u003c/html\u003e`;\nconst config = { rtl: true };\nconst posthtmlConfig = { sync: true };\n\nconst result = posthtmlRtl.process(inputHtml, config, posthtmlConfig);\nconsole.log(result.html);\n```\n\nOr in asynchronous way\n\n```js\nposthtmlRtl.process(inputHtml, config).then(result =\u003e {\n  console.log(result.html);\n});\n```\n\nOr as `posthtml` plugin\n\n```js\nconst posthtml = require(\"posthtml\");\nconst posthtmlRtl = require(\"posthtml-rtl\");\n\nconst inputHtml = `\u003chtml dir=\"ltr\"\u003e\u003c/html\u003e`;\n\nposthtml()\n  .use(posthtmlRtl(/* options */))\n  .process(inputHtml, {} /* posthtml options */)\n  .then(result =\u003e {\n    console.log(result.html);\n  });\n```\n\n### Options\n\n| Option | Type                      | Default Value              | Description                                                  |\n| ------ | ------------------------- | -------------------------- | ------------------------------------------------------------ |\n| rtl    | Boolean                   | true                       | if true the output will be converted to RTL                  |\n| map    | Object                    | written below this section | The map used to flip html attributes like `dir` and `align`  |\n| rtlcss | Function, array or object | Instance of `rtlcss`       | Provide a function to convert css to RTL or `rtlcss` options |\n\n#### Default attributes map\n\n```json\n{\n  \"dir\": {\n    \"ltr\": \"rtl\",\n    \"rtl\": \"ltr\"\n  },\n  \"align\": {\n    \"left\": \"right\",\n    \"right\": \"left\"\n  }\n}\n```\n\n## Control Output\n\nThe package provide a way to ignore, remove and overwrite attributes. by using dataset attributes `data-$dir-$action`. where `$dir` is `rtl` or `ltr` and `$action` is `remove`, `ignore` or attribute name to overwrite.\n\n### Ignoring\n\nTo ignore tag use `data-$dir-ignore` attribute, for example:\n\n```html\n\u003c!-- input --\u003e\n\u003chtml\u003e\n  \u003cstyle data-rtl-ignore\u003e\n    .foo {\n      float: left;\n    }\n  \u003c/style\u003e\n\u003c/html\u003e\n\n\u003c!-- RTL output --\u003e\n\u003chtml\u003e\n  \u003cstyle\u003e\n    .foo {\n      float: left;\n    }\n  \u003c/style\u003e\n\u003c/html\u003e\n```\n\nTo ignore specific attribute, you can pass a list of attributes to `data-$dir-ignore` separated by by space, for example:\n\n```html\n\u003c!-- input --\u003e\n\u003chtml dir=\"ltr\" align=\"left\" style=\"margin-left: 10px;\" data-rtl-ignore=\"dir align\"\u003e\u003c/html\u003e\n\n\u003c!-- RTL output --\u003e\n\u003chtml dir=\"ltr\" align=\"left\" style=\"margin-right: 10px;\"\u003e\u003c/html\u003e\n```\n\n### Removing\n\nTo remove tag use `data-$dir-remove` attribute, for example:\n\n```html\n\u003c!-- input --\u003e\n\u003cstyle data-ltr-remove\u003e\n  img {\n    transform: scaleX(-1);\n  }\n\u003c/style\u003e\n\u003cimg src=\"picture.jpg\" /\u003e\n\n\u003c!-- RTL output --\u003e\n\u003cstyle\u003e\n  img {\n    transform: scaleX(-1);\n  }\n\u003c/style\u003e\n\u003cimg src=\"picture.jpg\" /\u003e\n\n\u003c!-- LTR output --\u003e\n\u003cimg src=\"picture.jpg\" /\u003e\n```\n\nTo remove specific attribute, you can pass a list of attributes to `data-$dir-remove` separated by by space, for example:\n\n```html\n\u003c!-- input --\u003e\n\u003chtml dir=\"ltr\" data-ltr-remove=\"dir\"\u003e\u003c/html\u003e\n\n\u003c!-- RTL output --\u003e\n\u003chtml dir=\"rtl\"\u003e\u003c/html\u003e\n\n\u003c!-- LTR output --\u003e\n\u003chtml\u003e\u003c/html\u003e\n```\n\n### Overwriting\n\nYou can overwrite any attribute using `data-$dir-$attr` where `$attr` is the attribute that you want to overwrite, for example:\n\n```html\n\u003c!-- input --\u003e\n\u003cimg src=\"logo.png\" data-rtl-src=\"logo-ar.png\" /\u003e\n\n\u003c!-- RTL output --\u003e\n\u003cimg src=\"logo-ar.png\" /\u003e\n\n\u003c!-- LTR output --\u003e\n\u003cimg src=\"logo.png\" /\u003e\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fposthtml%2Fposthtml-rtl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fposthtml%2Fposthtml-rtl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fposthtml%2Fposthtml-rtl/lists"}