remark-smartypants
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -14,3 +14,3 @@ /** | ||
export default function remarkSmartypants( | ||
options?: void | Options | ||
options?: void | Options, | ||
): void | Transformer<Root, Root>; |
54
index.js
@@ -6,5 +6,5 @@ /** | ||
import { retext } from 'retext' | ||
import { visit } from 'unist-util-visit' | ||
import smartypants from 'retext-smartypants' | ||
import { retext } from "retext"; | ||
import { visit } from "unist-util-visit"; | ||
import smartypants from "retext-smartypants"; | ||
@@ -17,9 +17,43 @@ /** | ||
export default function remarkSmartypants(options) { | ||
const processor = retext().use(smartypants, options) | ||
const transformer = tree => { | ||
visit(tree, 'text', node => { | ||
node.value = String(processor.processSync(node.value)) | ||
}) | ||
} | ||
return transformer | ||
const processor = retext().use(smartypants, { | ||
...options, | ||
// Do not replace ellipses, dashes, backticks because they change string | ||
// length, and we couldn't guarantee right splice of text in second visit of | ||
// tree | ||
ellipses: false, | ||
dashes: false, | ||
backticks: false, | ||
}); | ||
const processor2 = retext().use(smartypants, { | ||
...options, | ||
// Do not replace quotes because they are already replaced in the first | ||
// processor | ||
quotes: false, | ||
}); | ||
return (tree) => { | ||
let allText = ""; | ||
let startIndex = 0; | ||
const nodes = []; | ||
visit(tree, ["text", "inlineCode"], (node) => { | ||
allText += | ||
node.type === "text" ? node.value : "A".repeat(node.value.length); | ||
nodes.push(node); | ||
}); | ||
// Concat all text into one string, to properly replace quotes around links | ||
// and bold text | ||
allText = processor.processSync(allText).value; | ||
for (const node of nodes) { | ||
const endIndex = startIndex + node.value.length; | ||
if (node.type === "text") { | ||
const processedText = allText.slice(startIndex, endIndex); | ||
node.value = processor2.processSync(processedText).value; | ||
} | ||
startIndex = endIndex; | ||
} | ||
}; | ||
} |
{ | ||
"name": "remark-smartypants", | ||
"description": "remark plugin to implement SmartyPants", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"license": "MIT", | ||
@@ -40,18 +40,28 @@ "files": [ | ||
"scripts": { | ||
"prepare": "husky install", | ||
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest", | ||
"setup": "npm install && simple-git-hooks", | ||
"test": "vitest run", | ||
"prepublishOnly": "npm test" | ||
}, | ||
"simple-git-hooks": { | ||
"pre-commit": "npx nano-staged", | ||
"pre-push": "npm test" | ||
}, | ||
"nano-staged": { | ||
"*.{js,ts,json}": [ | ||
"prettier --write" | ||
] | ||
}, | ||
"prettier": {}, | ||
"dependencies": { | ||
"retext": "^8.1.0", | ||
"retext-smartypants": "^5.1.0", | ||
"unist-util-visit": "^4.1.0" | ||
"retext-smartypants": "^5.2.0", | ||
"unist-util-visit": "^5.0.0" | ||
}, | ||
"devDependencies": { | ||
"cross-env": "^7.0.3", | ||
"husky": "^7.0.4", | ||
"jest": "^27.3.1", | ||
"prettier": "^2.4.1", | ||
"remark": "^14.0.2" | ||
"nano-staged": "^0.8.0", | ||
"prettier": "^3.1.1", | ||
"remark": "^14.0.3", | ||
"simple-git-hooks": "^2.9.0", | ||
"vitest": "^1.1.2" | ||
} | ||
} |
# remark-smartypants | ||
[![package version](https://badgen.net/npm/v/remark-smartypants)][npm] | ||
[![number of downloads](https://badgen.net/npm/dt/remark-smartypants)][npm] | ||
[remark] plugin to implement [SmartyPants]. Now with 100% more ESM! | ||
## Installing | ||
```sh | ||
# using npm | ||
npm install remark-smartypants | ||
# using yarn | ||
yarn add remark-smartypants | ||
``` | ||
## Usage | ||
Example using [remark]: | ||
```js | ||
import remark from 'remark' | ||
import smartypants from 'remakr-smartypants' | ||
import remark from "remark"; | ||
import smartypants from "remark-smartypants"; | ||
const result = await remark() | ||
.use(smartypants) | ||
.process('# <<Hello World!>>') | ||
const result = await remark().use(smartypants).process("# <<Hello World!>>"); | ||
console.log(String(result)) | ||
console.log(String(result)); | ||
// # «Hello World!» | ||
@@ -24,12 +35,12 @@ ``` | ||
```js | ||
import mdx from '@mdx-js/mdx' | ||
import smartypants from 'remark-smartypants' | ||
import mdx from "@mdx-js/mdx"; | ||
import smartypants from "remark-smartypants"; | ||
const result = await mdx('# <<Hello World!>>', { | ||
remarkPlugins: [ | ||
smartypants, | ||
], | ||
}) | ||
const result = await mdx("# ---Hello World!---", { | ||
remarkPlugins: [smartypants], | ||
}); | ||
``` | ||
Note that angle quotes in the former example (`<<...>>`) are probably impossible in MDX because there they are invalid syntax. | ||
This plugin uses [retext-smartypants](https://github.com/retextjs/retext-smartypants) under the hood, so it takes the same options: | ||
@@ -39,8 +50,13 @@ | ||
const result = await remark() | ||
.use(smartypants, { dashes: 'oldschool' }) | ||
.process('en dash (--), em dash (---)') | ||
.use(smartypants, { dashes: "oldschool" }) | ||
.process("en dash (--), em dash (---)"); | ||
``` | ||
## License | ||
[MIT License, Copyright (c) Matija Marohnić](./LICENSE) | ||
[npm]: https://www.npmjs.com/package/remark-smartypants | ||
[remark]: https://remark.js.org | ||
[SmartyPants]: https://daringfireball.net/projects/smartypants | ||
[MDX]: https://mdxjs.com |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6064
62
61
+ Added@types/unist@3.0.3(transitive)
+ Addedunist-util-is@6.0.0(transitive)
+ Addedunist-util-visit@5.0.0(transitive)
+ Addedunist-util-visit-parents@6.0.1(transitive)
Updatedretext-smartypants@^5.2.0
Updatedunist-util-visit@^5.0.0