Skip to content

Commit

Permalink
✍️ Abbreviations First Time Long (ftl) (#530)
Browse files Browse the repository at this point in the history
* use `rimraf` instead of `rm -rf`
* Adding the First Time Long (`firstTimeLong`) option to abbreviations
* Also added some additional testing for abbreviations (moved to tests folder with yaml cases)

---------

Co-authored-by: Rowan Cockett <rowanc1@gmail.com>
  • Loading branch information
jan-david-fischbach and rowanc1 committed Aug 4, 2023
1 parent fee1eea commit 30da1da
Show file tree
Hide file tree
Showing 16 changed files with 170 additions and 41 deletions.
5 changes: 5 additions & 0 deletions .changeset/silent-numbers-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'myst-transforms': minor
---

Add `firstTimeLong` option to the abbreviations transform to expand the abbreviation the first time it is encountered.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"build": "turbo run build",
"dev": "turbo run dev --parallel",
"clean": "turbo run clean --parallel && find . -name '.turbo' -type d -prune -exec rm -rf '{}' +",
"clean": "turbo run clean --parallel && find . -name '.turbo' -type d -prune -exec rimraf '{}' +",
"lint": "turbo run lint",
"lint:format": "turbo run lint:format",
"test": "turbo run test",
Expand Down
2 changes: 1 addition & 1 deletion packages/jats-to-myst/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"url": "git+https://github.com/executablebooks/mystmd.git"
},
"scripts": {
"clean": "rm -rf dist",
"clean": "rimraf dist",
"lint": "eslint \"src/**/*.ts\" -c .eslintrc.cjs --max-warnings 1",
"lint:format": "prettier --check src/*.ts src/**/*.ts",
"test": "vitest run",
Expand Down
2 changes: 1 addition & 1 deletion packages/myst-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"url": "git+https://github.com/executablebooks/mystmd.git"
},
"scripts": {
"clean": "rm -rf dist",
"clean": "rimraf dist",
"test": "vitest run",
"test:watch": "vitest watch",
"lint": "eslint \"src/**/*.ts\" -c .eslintrc.cjs",
Expand Down
2 changes: 1 addition & 1 deletion packages/myst-spec-ext/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"url": "git+https://github.com/executablebooks/mystmd.git"
},
"scripts": {
"clean": "rm -rf dist",
"clean": "rimraf dist",
"lint": "eslint \"src/**/*.ts\" -c .eslintrc.cjs --max-warnings 1",
"lint:format": "prettier --check src/*.ts src/**/*.ts",
"build:esm": "tsc --project ./tsconfig.json --module es2015 --outDir dist --declaration",
Expand Down
2 changes: 1 addition & 1 deletion packages/myst-templates/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"url": "git+https://github.com/executablebooks/mystmd.git"
},
"scripts": {
"clean": "rm -rf dist",
"clean": "rimraf dist",
"lint": "eslint \"src/**/*.ts\" -c .eslintrc.cjs",
"lint:format": "prettier --check src/*.ts src/**/*.ts",
"test": "vitest run",
Expand Down
2 changes: 1 addition & 1 deletion packages/myst-to-html/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"url": "git+https://github.com/executablebooks/mystmd.git"
},
"scripts": {
"clean": "rm -rf dist",
"clean": "rimraf dist",
"lint": "eslint \"src/**/*.ts\" -c .eslintrc.cjs --max-warnings 1",
"lint:format": "prettier --check src/*.ts src/**/*.ts",
"test": "vitest run",
Expand Down
2 changes: 1 addition & 1 deletion packages/myst-to-jats/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"url": "git+https://github.com/executablebooks/mystmd.git"
},
"scripts": {
"clean": "rm -rf dist",
"clean": "rimraf dist",
"lint": "eslint \"src/**/*.ts\" -c .eslintrc.cjs --max-warnings 1",
"lint:format": "prettier --check src/*.ts src/**/*.ts",
"test": "vitest run",
Expand Down
2 changes: 1 addition & 1 deletion packages/myst-to-md/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"url": "git+https://github.com/executablebooks/mystmd.git"
},
"scripts": {
"clean": "rm -rf dist",
"clean": "rimraf dist",
"lint": "eslint \"src/**/*.ts\" -c .eslintrc.cjs --max-warnings 1",
"lint:format": "prettier --check src/*.ts src/**/*.ts",
"test": "vitest run",
Expand Down
2 changes: 1 addition & 1 deletion packages/myst-to-tex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"url": "git+https://github.com/executablebooks/mystmd.git"
},
"scripts": {
"clean": "rm -rf dist",
"clean": "rimraf dist",
"lint": "eslint \"src/**/*.ts\" -c .eslintrc.cjs --max-warnings 1",
"lint:format": "prettier --check src/*.ts src/**/*.ts",
"test": "vitest run",
Expand Down
2 changes: 1 addition & 1 deletion packages/myst-to-typst/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"url": "git+https://github.com/executablebooks/mystmd.git"
},
"scripts": {
"clean": "rm -rf dist",
"clean": "rimraf dist",
"lint": "eslint \"src/**/*.ts\" -c .eslintrc.cjs --max-warnings 1",
"lint:format": "prettier --check src/*.ts src/**/*.ts",
"test": "vitest run",
Expand Down
29 changes: 0 additions & 29 deletions packages/myst-transforms/src/abbreviations.spec.ts

This file was deleted.

20 changes: 19 additions & 1 deletion packages/myst-transforms/src/abbreviations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Plugin } from 'unified';
import type { Root, StaticPhrasingContent } from 'mdast';
import type { Root, StaticPhrasingContent, Text } from 'mdast';
import { toText } from 'myst-common';
import { selectAll } from 'unist-util-select';
import type { Abbreviation } from 'myst-spec';
Expand All @@ -8,7 +8,14 @@ import type { FindAndReplaceSchema, RegExpMatchObject } from 'mdast-util-find-an
import { findAndReplace } from 'mdast-util-find-and-replace';

type Options = {
/** An object of abbreviations { "TLA": "Three Letter Acronym" } */
abbreviations?: Record<string, string>;
/**
* Expand the abbreviation the first time it is encountered,
*
* i.e. `TLA` --> `Three Letter Acronym (TLA)`
*/
firstTimeLong?: boolean;
};

// We will not replace abbreviation text inside of these nodes
Expand Down Expand Up @@ -44,6 +51,17 @@ export function abbreviationTransform(mdast: Root, opts?: Options) {
if (title) node.title = title;
});
replaceText(mdast, opts);

if (opts.firstTimeLong) {
const new_abbreviations = selectAll('abbreviation', mdast) as Abbreviation[];
const explained = new Set();
new_abbreviations.forEach((node) => {
if (explained.has(node.title)) return;
explained.add(node.title);
const short = node.children[0] as unknown as Text;
short.value = `${node.title} (${short.value})`;
});
}
}

export const abbreviationPlugin: Plugin<[Options], Root, Root> = (opts) => (tree) => {
Expand Down
32 changes: 32 additions & 0 deletions packages/myst-transforms/tests/abbreviations.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, expect, test } from 'vitest';
import fs from 'node:fs';
import path from 'node:path';
import yaml from 'js-yaml';
import type { Root } from 'mdast';
import { abbreviationTransform, ReferenceState } from '../src';

type TestFile = {
cases: TestCase[];
};
type TestCase = {
title: string;
before: Root;
after: Root;
opts?: Record<string, boolean>;
};

const fixtures = path.join('tests', 'abbreviations.yml');

const testYaml = fs.readFileSync(fixtures).toString();
const cases = (yaml.load(testYaml) as TestFile).cases;

describe('abbreviate', () => {
test.each(cases.map((c): [string, TestCase] => [c.title, c]))(
'%s',
(_, { before, after, opts }) => {
abbreviationTransform(before as Root, opts);

expect(yaml.dump(before)).toEqual(yaml.dump(after));
},
);
});
103 changes: 103 additions & 0 deletions packages/myst-transforms/tests/abbreviations.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
cases:
- title: simple abbreviation replacement
opts:
abbreviations:
MyST: Markedly Structured Text
before:
type: root
children:
- type: blockquote
children:
- type: paragraph
children:
- type: link
children:
- type: text
value: Link with MyST in it
- type: text
value: This is about MyST Markdown
after:
type: root
children:
- type: blockquote
children:
- type: paragraph
children:
- type: link
children:
- type: text
value: Link with MyST in it
- type: text
value: 'This is about '
- type: abbreviation
title: Markedly Structured Text
children:
- type: text
value: MyST
- type: text
value: ' Markdown'
- title: Abbreviation in heading
opts:
abbreviations:
MyST: Markedly Structured Text
before:
type: root
children:
- type: heading
depth: 1
children:
- type: text
value: Testing MyST
after:
type: root
children:
- type: heading
depth: 1
children:
- type: text
value: 'Testing '
- type: abbreviation
title: Markedly Structured Text
children:
- type: text
value: MyST
- title: Abbreviation first time long
opts:
firstTimeLong: true
abbreviations:
MyST: Markedly Structured Text
before:
type: root
children:
- type: text
value: 'Testing MyST does it render out long? Does MyST now?'
- type: text
value: 'or maybe now: MyST?'
after:
type: root
children:
- type: text
value: 'Testing '
- type: abbreviation
title: Markedly Structured Text
children:
- type: text
value: Markedly Structured Text (MyST)
- type: text
value: ' does it render out long? Does '
- type: abbreviation
title: Markedly Structured Text
children:
- type: text
value: MyST
- type: text
value: ' now?'
- type: text
value: 'or maybe now: '
- type: abbreviation
title: Markedly Structured Text
children:
- type: text
value: MyST
- type: text
value: '?'
2 changes: 1 addition & 1 deletion packages/tex-to-myst/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"url": "git+https://github.com/executablebooks/mystmd.git"
},
"scripts": {
"clean": "rm -rf dist",
"clean": "rimraf dist",
"lint": "eslint \"src/**/*.ts\" -c .eslintrc.cjs --max-warnings 1",
"lint:format": "prettier --check src/*.ts src/**/*.ts",
"test": "vitest run",
Expand Down

0 comments on commit 30da1da

Please sign in to comment.