Skip to content

Commit

Permalink
Support GitHub alerts in tag annotations
Browse files Browse the repository at this point in the history
Closes #126
  • Loading branch information
ezzatron committed Mar 7, 2024
1 parent 06f9b54 commit 7d31410
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 3 deletions.
6 changes: 6 additions & 0 deletions dist/main.js

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

4 changes: 2 additions & 2 deletions dist/main.js.map

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion src/markdown.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import { Node } from "mdast";
import { remark } from "remark";
import remarkGfm from "remark-gfm";
import { visit } from "unist-util-visit";

const ALERT_PATTERN =
/(\[!(?:CAUTION|IMPORTANT|NOTE|TIP|WARNING)])(?!\s*$)(\s*)/gm;
const SOFT_BREAK_PATTERN = /$[^$]/gms;

export function createProcessor(): (original: string) => Promise<string> {
const createRemark = remark()
.use(remarkGfm)
// strip soft breaks
.use(() => {
return (tree) => {
// strip soft breaks
visit(tree, "text", (node: { value: string }) => {
node.value = node.value.replace(SOFT_BREAK_PATTERN, " ");
});

// preserve GitHub alerts
visit(tree, "blockquote", (node: Node) => {
visit(node, "text", (node: { value: string }) => {
node.value = node.value.replace(ALERT_PATTERN, "$1\n");
});
});
};
})
.freeze();
Expand Down
17 changes: 17 additions & 0 deletions test/fixture/markdown-processor/gfm/expected.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# GFM

## Alerts

> \[!NOTE]
> Useful information that users should know, even when skimming content.
> \[!TIP]
> Helpful advice for doing things better or more easily.
> \[!IMPORTANT]
> Key information users need to know to achieve their goal.
> \[!WARNING]
> Urgent info that needs immediate user attention to avoid problems.
> \[!CAUTION]
> Advises about risks or negative outcomes of certain actions.
## Autolink literals

[www.example.com](http://www.example.com), <https://example.com>, and <[email protected]>.
Expand Down
17 changes: 17 additions & 0 deletions test/fixture/markdown-processor/gfm/input.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# GFM

## Alerts

> [!NOTE]
> Useful information that users should know, even when skimming content.
> [!TIP]
> Helpful advice for doing things better or more easily.
> [!IMPORTANT]
> Key information users need to know to achieve their goal.
> [!WARNING]
> Urgent info that needs immediate user attention to avoid problems.
> [!CAUTION]
> Advises about risks or negative outcomes of certain actions.
## Autolink literals

www.example.com, https://example.com, and [email protected].
Expand Down
4 changes: 4 additions & 0 deletions test/suite/e2e/happy-path.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ one
paragraph
@actions
> [!IMPORTANT]
> this should be an alert
`;

const config = `assets:
Expand Down Expand Up @@ -186,6 +189,7 @@ paragraph
${"markdown heading 2"} | ${`//h2[normalize-space()='Heading 2']`}
${"markdown paragraphs"} | ${`//*[normalize-space()='this should form one paragraph']`}
${"mention"} | ${`//a[@href='https://github.com/actions'][normalize-space()='@actions']`}
${"alert"} | ${`//*[contains(concat(' ', normalize-space(@class), ' '), ' markdown-alert-important ')]/p[not(contains(concat(' ', normalize-space(@class), ' '), ' markdown-alert-title '))][normalize-space()='this should be an alert']`}
${"release notes"} | ${`//*[normalize-space()='Full Changelog: https://github.com/${owner}/${repo}/commits/${tagName}']`}
`(
"should produce the expected release body elements ($description)",
Expand Down

0 comments on commit 7d31410

Please sign in to comment.