Quartz sync: Mar 1, 2025, 2:26 PM

This commit is contained in:
Anthony Berg
2025-03-01 14:26:36 +01:00
parent 16c38cf522
commit 8fb1f81784
19490 changed files with 2553421 additions and 0 deletions

View File

@@ -0,0 +1,7 @@
import { type Options } from "retext-smartypants";
import type { Plugin } from "unified";
/**
* remark plugin to implement SmartyPants.
*/
declare const remarkSmartypants: Plugin<[Options?]>;
export default remarkSmartypants;

View File

@@ -0,0 +1,76 @@
import { retext } from "retext";
import { visit } from "unist-util-visit";
import smartypants from "retext-smartypants";
const VISITED_NODES = new Set(["text", "inlineCode", "paragraph"]);
const IGNORED_HTML_ELEMENTS = new Set(["style", "script"]);
const check = (node, index, parent) => {
return (parent &&
(parent.type !== "mdxJsxTextElement" ||
("name" in parent &&
typeof parent.name === "string" &&
!IGNORED_HTML_ELEMENTS.has(parent.name))) &&
VISITED_NODES.has(node.type) &&
(isLiteral(node) || isParagraph(node)));
};
/**
* remark plugin to implement SmartyPants.
*/
const remarkSmartypants = (options) => {
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, check, (node) => {
if (isLiteral(node)) {
allText +=
node.type === "text" ? node.value : "A".repeat(node.value.length);
}
else if (isParagraph(node)) {
// Inject a "fake" space because otherwise, when concatenated below,
// smartypants will fail to recognize opening quotes at the start of
// paragraphs
allText += " ";
}
nodes.push(node);
});
// Concat all text into one string, to properly replace quotes around links
// and bold text
allText = processor.processSync(allText).toString();
for (const node of nodes) {
if (isLiteral(node)) {
const endIndex = startIndex + node.value.length;
if (node.type === "text") {
const processedText = allText.slice(startIndex, endIndex);
node.value = processor2.processSync(processedText).toString();
}
startIndex = endIndex;
}
else if (isParagraph(node)) {
// Skip over the space we added above
startIndex += 1;
}
}
};
};
function isLiteral(node) {
return "value" in node && typeof node.value === "string";
}
function isParagraph(node) {
return node.type === "paragraph";
}
export default remarkSmartypants;

View File

@@ -0,0 +1,9 @@
MIT License
Copyright (c) Matija Marohnić <matija.marohnic@gmail.com> (silvenon.com)
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:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.

View File

@@ -0,0 +1,60 @@
{
"name": "remark-smartypants",
"description": "remark plugin to implement SmartyPants",
"version": "3.0.2",
"license": "MIT",
"files": [
"./dist",
"!dist/tsconfig.build.tsbuildinfo"
],
"exports": "./dist/plugin.js",
"types": "./dist/plugin.d.ts",
"type": "module",
"engines": {
"node": ">=16.0.0"
},
"repository": {
"type": "git",
"url": "https://github.com/silvenon/remark-smartypants.git"
},
"author": {
"name": "Matija Marohnić",
"email": "matija.marohnic@gmail.com",
"url": "https://silvenon.com"
},
"homepage": "https://github.com/silvenon/remark-smartypants#readme",
"bugs": {
"url": "https://github.com/silvenon/remark-smartypants/issues",
"email": "matija.marohnic@gmail.com"
},
"keywords": [
"unified",
"remark",
"remark-plugin",
"smartypants",
"punctuation",
"typography",
"smart"
],
"scripts": {
"test": "vitest run",
"build": "tsc --build",
"prepublishOnly": "npm test && npm run build"
},
"dependencies": {
"retext": "^9.0.0",
"retext-smartypants": "^6.0.0",
"unified": "^11.0.4",
"unist-util-visit": "^5.0.0"
},
"devDependencies": {
"@types/unist": "^3.0.2",
"lefthook": "^1.6.10",
"prettier": "^3.2.5",
"remark": "^15.0.1",
"remark-mdx": "^3.0.1",
"typescript": "^5.4.5",
"unist-util-is": "^6.0.0",
"vitest": "^1.5.0"
}
}

View File

@@ -0,0 +1,60 @@
# 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 "remark-smartypants";
const result = await remark().use(smartypants).process("# <<Hello World!>>");
console.log(String(result));
// # «Hello World!»
```
I created this plugin because I wanted to add SmartyPants to [MDX]:
```js
import mdx from "@mdx-js/mdx";
import smartypants from "remark-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:
```js
const result = await remark()
.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