├── .npmrc ├── .prettierrc.js ├── .gitignore ├── tsconfig.json ├── tsconfig.dev.json ├── tsconfig.build.json ├── tsconfig.base.json ├── lefthook.yml ├── .github └── workflows │ └── main.yml ├── license ├── package.json ├── readme.md ├── plugin.ts └── plugin.test.ts /.npmrc: -------------------------------------------------------------------------------- 1 | engine-strict=true 2 | -------------------------------------------------------------------------------- /.prettierrc.js: -------------------------------------------------------------------------------- 1 | export default {}; 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | *.log 4 | .vscode 5 | .idea/ 6 | 7 | /dist 8 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "include": [], 3 | "references": [ 4 | { "path": "./tsconfig.build.json" }, 5 | { "path": "./tsconfig.dev.json" } 6 | ] 7 | } 8 | -------------------------------------------------------------------------------- /tsconfig.dev.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.base.json", 3 | "compilerOptions": { 4 | "noEmit": true, 5 | "allowImportingTsExtensions": true 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.base.json", 3 | "compilerOptions": { 4 | "composite": true, 5 | "outDir": "./dist", 6 | "declaration": true 7 | }, 8 | "files": ["./plugin.ts"] 9 | } 10 | -------------------------------------------------------------------------------- /tsconfig.base.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "Node16", 4 | "esModuleInterop": true, 5 | "target": "ES2021", 6 | "lib": ["ES2021"], 7 | "skipLibCheck": true, 8 | "strict": true 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /lefthook.yml: -------------------------------------------------------------------------------- 1 | pre-commit: 2 | piped: true 3 | commands: 4 | prettier: 5 | priority: 1 6 | run: npx prettier --write --ignore-unknown {staged_files} 7 | stage_fixed: true 8 | 9 | pre-push: 10 | parallel: true 11 | commands: 12 | test: 13 | run: npm test 14 | typescript: 15 | run: npx tsc --build 16 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | pull_request: {} 8 | 9 | jobs: 10 | prettier: 11 | name: Prettier 12 | runs-on: ubuntu-latest 13 | steps: 14 | - name: Check out repository 15 | uses: actions/checkout@v3 16 | - name: Set up Node.js 17 | uses: actions/setup-node@v3 18 | with: 19 | node-version-file: package.json 20 | - name: Install dependencies 21 | run: npm ci 22 | - name: Run Prettier 23 | run: npx prettier --check . 24 | vitest: 25 | name: Vitest 26 | runs-on: ubuntu-latest 27 | steps: 28 | - name: Check out repository 29 | uses: actions/checkout@v3 30 | - name: Set up Node.js 31 | uses: actions/setup-node@v3 32 | with: 33 | node-version-file: package.json 34 | - name: Install dependencies 35 | run: npm ci 36 | - name: Run tests 37 | run: npm test 38 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Matija Marohnić (silvenon.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "remark-smartypants", 3 | "description": "remark plugin to implement SmartyPants", 4 | "version": "3.0.2", 5 | "license": "MIT", 6 | "files": [ 7 | "./dist", 8 | "!dist/tsconfig.build.tsbuildinfo" 9 | ], 10 | "exports": "./dist/plugin.js", 11 | "types": "./dist/plugin.d.ts", 12 | "type": "module", 13 | "engines": { 14 | "node": ">=16.0.0" 15 | }, 16 | "repository": { 17 | "type": "git", 18 | "url": "https://github.com/silvenon/remark-smartypants.git" 19 | }, 20 | "author": { 21 | "name": "Matija Marohnić", 22 | "email": "matija.marohnic@gmail.com", 23 | "url": "https://silvenon.com" 24 | }, 25 | "homepage": "https://github.com/silvenon/remark-smartypants#readme", 26 | "bugs": { 27 | "url": "https://github.com/silvenon/remark-smartypants/issues", 28 | "email": "matija.marohnic@gmail.com" 29 | }, 30 | "keywords": [ 31 | "unified", 32 | "remark", 33 | "remark-plugin", 34 | "smartypants", 35 | "punctuation", 36 | "typography", 37 | "smart" 38 | ], 39 | "scripts": { 40 | "test": "vitest run", 41 | "build": "tsc --build", 42 | "prepublishOnly": "npm test && npm run build" 43 | }, 44 | "dependencies": { 45 | "retext": "^9.0.0", 46 | "retext-smartypants": "^6.0.0", 47 | "unified": "^11.0.4", 48 | "unist-util-visit": "^5.0.0" 49 | }, 50 | "devDependencies": { 51 | "@types/unist": "^3.0.2", 52 | "lefthook": "^1.6.10", 53 | "prettier": "^3.2.5", 54 | "remark": "^15.0.1", 55 | "remark-mdx": "^3.0.1", 56 | "typescript": "^5.4.5", 57 | "unist-util-is": "^6.0.0", 58 | "vitest": "^1.5.0" 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # remark-smartypants 2 | 3 | [![package version](https://badgen.net/npm/v/remark-smartypants)][npm] 4 | [![number of downloads](https://badgen.net/npm/dt/remark-smartypants)][npm] 5 | 6 | [remark] plugin to implement [SmartyPants]. Now with 100% more ESM! 7 | 8 | ## Installing 9 | 10 | ```sh 11 | # using npm 12 | npm install remark-smartypants 13 | 14 | # using yarn 15 | yarn add remark-smartypants 16 | ``` 17 | 18 | ## Usage 19 | 20 | Example using [remark]: 21 | 22 | ```js 23 | import remark from "remark"; 24 | import smartypants from "remark-smartypants"; 25 | 26 | const result = await remark().use(smartypants).process("# <>"); 27 | 28 | console.log(String(result)); 29 | // # «Hello World!» 30 | ``` 31 | 32 | I created this plugin because I wanted to add SmartyPants to [MDX]: 33 | 34 | ```js 35 | import mdx from "@mdx-js/mdx"; 36 | import smartypants from "remark-smartypants"; 37 | 38 | const result = await mdx("# ---Hello World!---", { 39 | remarkPlugins: [smartypants], 40 | }); 41 | ``` 42 | 43 | Note that angle quotes in the former example (`<<...>>`) are probably impossible in MDX because there they are invalid syntax. 44 | 45 | This plugin uses [retext-smartypants](https://github.com/retextjs/retext-smartypants) under the hood, so it takes the same options: 46 | 47 | ```js 48 | const result = await remark() 49 | .use(smartypants, { dashes: "oldschool" }) 50 | .process("en dash (--), em dash (---)"); 51 | ``` 52 | 53 | ## License 54 | 55 | [MIT License, Copyright (c) Matija Marohnić](./LICENSE) 56 | 57 | [npm]: https://www.npmjs.com/package/remark-smartypants 58 | [remark]: https://remark.js.org 59 | [SmartyPants]: https://daringfireball.net/projects/smartypants 60 | [MDX]: https://mdxjs.com 61 | -------------------------------------------------------------------------------- /plugin.ts: -------------------------------------------------------------------------------- 1 | import { retext } from "retext"; 2 | import { visit } from "unist-util-visit"; 3 | import smartypants, { type Options } from "retext-smartypants"; 4 | import type { Plugin } from "unified"; 5 | import type { Test } from "unist-util-is"; 6 | import type { Node } from "unist"; 7 | 8 | const VISITED_NODES = new Set(["text", "inlineCode", "paragraph"]); 9 | 10 | const IGNORED_HTML_ELEMENTS = new Set(["style", "script"]); 11 | 12 | const check: Test = (node, index, parent) => { 13 | return ( 14 | parent && 15 | (parent.type !== "mdxJsxTextElement" || 16 | ("name" in parent && 17 | typeof parent.name === "string" && 18 | !IGNORED_HTML_ELEMENTS.has(parent.name))) && 19 | VISITED_NODES.has(node.type) && 20 | (isLiteral(node) || isParagraph(node)) 21 | ); 22 | }; 23 | 24 | /** 25 | * remark plugin to implement SmartyPants. 26 | */ 27 | const remarkSmartypants: Plugin<[Options?]> = (options) => { 28 | const processor = retext().use(smartypants, { 29 | ...options, 30 | // Do not replace ellipses, dashes, backticks because they change string 31 | // length, and we couldn't guarantee right splice of text in second visit of 32 | // tree 33 | ellipses: false, 34 | dashes: false, 35 | backticks: false, 36 | }); 37 | 38 | const processor2 = retext().use(smartypants, { 39 | ...options, 40 | // Do not replace quotes because they are already replaced in the first 41 | // processor 42 | quotes: false, 43 | }); 44 | 45 | return (tree) => { 46 | let allText = ""; 47 | let startIndex = 0; 48 | const nodes: (Literal | Paragraph)[] = []; 49 | 50 | visit(tree, check, (node) => { 51 | if (isLiteral(node)) { 52 | allText += 53 | node.type === "text" ? node.value : "A".repeat(node.value.length); 54 | } else if (isParagraph(node)) { 55 | // Inject a "fake" space because otherwise, when concatenated below, 56 | // smartypants will fail to recognize opening quotes at the start of 57 | // paragraphs 58 | allText += " "; 59 | } 60 | nodes.push(node); 61 | }); 62 | 63 | // Concat all text into one string, to properly replace quotes around links 64 | // and bold text 65 | allText = processor.processSync(allText).toString(); 66 | 67 | for (const node of nodes) { 68 | if (isLiteral(node)) { 69 | const endIndex = startIndex + node.value.length; 70 | if (node.type === "text") { 71 | const processedText = allText.slice(startIndex, endIndex); 72 | node.value = processor2.processSync(processedText).toString(); 73 | } 74 | startIndex = endIndex; 75 | } else if (isParagraph(node)) { 76 | // Skip over the space we added above 77 | startIndex += 1; 78 | } 79 | } 80 | }; 81 | }; 82 | 83 | // the Literal interface from @types/unist has unknown value, we want string 84 | interface Literal extends Node { 85 | value: string; 86 | } 87 | 88 | function isLiteral(node: Node): node is Literal { 89 | return "value" in node && typeof node.value === "string"; 90 | } 91 | 92 | interface Paragraph extends Node {} 93 | 94 | function isParagraph(node: Node): node is Paragraph { 95 | return node.type === "paragraph"; 96 | } 97 | 98 | export default remarkSmartypants; 99 | -------------------------------------------------------------------------------- /plugin.test.ts: -------------------------------------------------------------------------------- 1 | import { it, expect, describe } from "vitest"; 2 | import { remark } from "remark"; 3 | import remarkMdx from "remark-mdx"; 4 | import remarkSmartypants from "./plugin.ts"; 5 | 6 | const compiler = remark().use(remarkSmartypants); 7 | const process = compiler.process.bind(compiler); 8 | 9 | it("implements SmartyPants", async () => { 10 | const file = await process('# "Hello World!"'); 11 | expect(String(file.toString())).toMatchInlineSnapshot(` 12 | "# “Hello World!” 13 | "`); 14 | }); 15 | 16 | it("handles quotes around links", async () => { 17 | const file = await process( 18 | `"wow". go to '[single](/foo)' today "[double](/bar)". . .`, 19 | ); 20 | expect(file.toString()).toMatchInlineSnapshot(` 21 | "“wow”. go to ‘[single](/foo)’ today “[double](/bar)”… 22 | "`); 23 | }); 24 | 25 | it("handles quotes around bold text", async () => { 26 | const file = await process( 27 | `foo '**Bolded -- \`\`part** of --- this quote' bar`, 28 | ); 29 | expect(file.toString()).toMatchInlineSnapshot(` 30 | "foo ‘**Bolded — “part** of --- this quote’ bar 31 | "`); 32 | }); 33 | 34 | describe("handles quotes around inline code", async () => { 35 | it("around inline code", async () => { 36 | const file = await process('"`code`"'); 37 | expect(file.toString()).toMatchInlineSnapshot(` 38 | "“\`code\`” 39 | "`); 40 | }); 41 | it("around inline code and text", async () => { 42 | const file = await process(`"\`single 'quote'. . .\` baz"`); 43 | expect(file.toString()).toMatchInlineSnapshot(` 44 | "“\`single 'quote'. . .\` baz” 45 | "`); 46 | }); 47 | 48 | it("around inline code with single quote", async () => { 49 | const file = await process("'`singles'`'"); 50 | expect(file.toString()).toMatchInlineSnapshot(` 51 | "‘\`singles'\`’ 52 | "`); 53 | }); 54 | 55 | it("around inline code with double quote", async () => { 56 | const file = await process('"`double"`"'); 57 | expect(file.toString()).toMatchInlineSnapshot(` 58 | "“\`double"\`” 59 | "`); 60 | }); 61 | }); 62 | 63 | describe("handles quotes at the edges of a paragraph", () => { 64 | it("at start after another paragraph", async () => { 65 | const file = await process('paragraph\n\n"after paragraph"'); 66 | expect(file.toString()).toMatchInlineSnapshot(` 67 | "paragraph\n\n“after paragraph” 68 | "`); 69 | }); 70 | 71 | it("at start after a blockquote", async () => { 72 | const file = await process('> blockquote\n\n"after blockquote"'); 73 | expect(file.toString()).toMatchInlineSnapshot(` 74 | "> blockquote\n\n“after blockquote” 75 | "`); 76 | }); 77 | 78 | it("at start within a blockquote", async () => { 79 | const file = await process('> blockquote\n>\n> "within blockquote"'); 80 | expect(file.toString()).toMatchInlineSnapshot(` 81 | "> blockquote\n>\n> “within blockquote” 82 | "`); 83 | }); 84 | 85 | it("at end before another paragraph", async () => { 86 | const file = await process('"before paragraph"\n\nparagraph'); 87 | expect(file.toString()).toMatchInlineSnapshot(` 88 | "“before paragraph”\n\nparagraph 89 | "`); 90 | }); 91 | 92 | it("at end before a blockquote", async () => { 93 | const file = await process('"before blockquote"\n\n> blockquote'); 94 | expect(file.toString()).toMatchInlineSnapshot(` 95 | "“before blockquote”\n\n> blockquote 96 | "`); 97 | }); 98 | }); 99 | 100 | describe("should ignore parent nodes", () => { 101 | const mdxCompiler = remark().use(remarkMdx).use(remarkSmartypants); 102 | const process = mdxCompiler.process.bind(mdxCompiler); 103 | 104 | it("`; 106 | const file = await process(mdxContent); 107 | expect(file.toString().trimEnd()).toBe(mdxContent); 108 | }); 109 | it("'; 111 | const file = await process(mdxContent); 112 | expect(file.toString().trimEnd()).toBe(mdxContent); 113 | }); 114 | }); 115 | --------------------------------------------------------------------------------