Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ An assortment of extensions and plugins for use with [the remark Markdown proces
* [remark-kbd-nested](./packages/remark-kbd-nested/)
* [remark-textras](./packages/remark-textras/)

There are some duplicated helper functions between remark-editorial-elements and remark-textras since I haven’t yet decided where to put them after moving them from [my blog](https://shivjm.blog/).
Some packages were only just moved from [my blog](https://shivjm.blog/), so there are duplicated helper functions and they’re missing tests & documentation.
6 changes: 6 additions & 0 deletions packages/remark-textras/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ This page is built using :abbr[HTML] and :abbr[CSS].
At this point, the program should fail with an error like :samp[Invalid fizz provided].
```

### `:work`

```markdown
See the aforementioned :work[A Real Pain] and :work[Futurama]{year="1999"}.
```

## API

TODO
Expand Down
1 change: 1 addition & 0 deletions packages/remark-textras/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"prepublish": "npm run build"
},
"dependencies": {
"hastscript": "^9.0.1",
"unist-util-visit": "^5.0.0"
},
"devDependencies": {
Expand Down
65 changes: 65 additions & 0 deletions packages/remark-textras/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Root, Parent } from "hast";
import { h } from "hastscript";
import { visit } from "unist-util-visit";
import { getFirstAndLastText, addClass } from "./ast.js";
// This is required to augment the AST type, but unfortunately can’t
Expand All @@ -8,6 +9,23 @@ import { getFirstAndLastText, addClass } from "./ast.js";
const OPENING_QUOTE = /^(?:‘|“|’|”)/;
const CLOSING_QUOTE = /(?:‘|“|’|”)$/;

const YEAR_SPACE = Object.freeze({ type: "text", value: " " });

const QUOTE_SPACE = Object.freeze({
type: "text",
value: "\u202F",
});

const OPENING_QUOTE_AST = Object.freeze({
type: "text",
value: "‘",
});

const CLOSING_QUOTE_AST = Object.freeze({
type: "text",
value: "’",
});

export function textrasPlugin() {
return transformer;

Expand Down Expand Up @@ -44,9 +62,56 @@ export function textrasPlugin() {
}
}

function transformWorkTitle(node: Parent) {
node.data = {
...node.data,
hName: "cite",
hProperties: { class: "work-title" },
};

const { year, quoted } = (node as any).attributes || {};

if (quoted !== undefined) {
const [first, last] = getFirstAndLastText(node);

if (OPENING_QUOTE.test(first ?? "")) {
node.children.unshift(QUOTE_SPACE);
}

node.children.unshift(OPENING_QUOTE_AST);

if (CLOSING_QUOTE.test(last ?? "")) {
node.children.push(QUOTE_SPACE);
}

node.children.push(CLOSING_QUOTE_AST);
}

if (year !== undefined) {
const time = h("time");
time.data = {
hName: "time",
hProperties: {
class: "work-vintage",
datetime: year,
},
};

time.children = [
{
type: "text",
value: `(${year})`,
},
];
node.children.push(YEAR_SPACE);
node.children.push(time);
}
}

function transformer(tree: Root, _file: unknown) {
visit(tree, { type: "textDirective", name: "ordinal" }, ordinalVisitor);
visit(tree, { type: "textDirective", name: "quote" }, quoteVisitor);
visit(tree, { type: "textDirective", name: "work" }, transformWorkTitle);

for (const [name, element] of Object.entries({
var: "var",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.