-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use
datasworn:
scheme for Datasworn ID links
Fixes #383
- Loading branch information
Showing
10 changed files
with
128 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,65 @@ | ||
import { extractDataswornLinkParts } from "./id"; | ||
import { | ||
extractDataswornLinkParts, | ||
matchDataswornLink, | ||
ParsedDataswornId, | ||
} from "./id"; | ||
|
||
describe("extractDataswornLinkParts", () => { | ||
it.each([ | ||
["asset:starforged/path/empath", ["asset", "starforged/path/empath"]], | ||
const VALID_TEST_CASES: [string, ParsedDataswornId][] = [ | ||
[ | ||
"asset:starforged/path/empath", | ||
{ | ||
kind: "asset", | ||
path: "starforged/path/empath", | ||
id: "asset:starforged/path/empath", | ||
}, | ||
], | ||
[ | ||
"asset.ability.move:starforged/path/empath.0.read_heart", | ||
["asset.ability.move", "starforged/path/empath.0.read_heart"], | ||
{ | ||
kind: "asset.ability.move", | ||
path: "starforged/path/empath.0.read_heart", | ||
id: "asset.ability.move:starforged/path/empath.0.read_heart", | ||
}, | ||
], | ||
]; | ||
|
||
it.each(VALID_TEST_CASES)("matches old-style link '%s'", (link, result) => { | ||
expect(extractDataswornLinkParts(link)).toEqual(result); | ||
}); | ||
|
||
it.each(VALID_TEST_CASES)( | ||
"matches new-style link 'datasworn:%s'", | ||
(link, result) => { | ||
expect(extractDataswornLinkParts("datasworn:" + link)).toEqual(result); | ||
}, | ||
); | ||
|
||
it.each([ | ||
["http://asdf", null], | ||
["./foo", null], | ||
])("should properly handle '%s'", (link, result) => { | ||
])("returns null for '%s'", (link, result) => { | ||
expect(extractDataswornLinkParts(link)).toEqual(result); | ||
}); | ||
}); | ||
|
||
describe("matchDataswornLink", () => { | ||
it.each` | ||
text | result | ||
${"[Foo](datasworn:asset:starforged/path/empath)"} | ${{ label: "Foo", id: "asset:starforged/path/empath" }} | ||
${"[Foo With Spaces -](asset:starforged/path/empath)"} | ${{ label: "Foo With Spaces -", id: "asset:starforged/path/empath" }} | ||
${"[Foo](https://foo)"} | ${null} | ||
${"datasworn:asset:starforged/path/empath"} | ${null} | ||
`( | ||
"should handle '%s'", | ||
({ | ||
text, | ||
result, | ||
}: { | ||
text: string; | ||
result: ReturnType<typeof matchDataswornLink>; | ||
}) => { | ||
expect(matchDataswornLink(text)).toEqual(result); | ||
}, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,56 @@ | ||
import { TypeId } from "@datasworn/core/dist/IdElements"; | ||
|
||
// TODO(@cwegrzyn): is there an official low complexity regex for datasworn ids? | ||
export const DATASWORN_ID_REGEX = new RegExp( | ||
String.raw`^((?:${TypeId.Primary.join("|")})(?:\.[-\w]+)*):(\w[-\w /\.]+)$`, | ||
const DATASWORN_ID_REGEX_NO_ANCHOR = new RegExp( | ||
String.raw`((?:${TypeId.Primary.join("|")})(?:\.[-\w]+)*):(\w[-\w /\.]+)`, | ||
); | ||
|
||
export const DATASWORN_ID_REGEX_NO_ANCHOR = new RegExp( | ||
String.raw`((?:${TypeId.Primary.join("|")})(?:\.[-\w]+)*):(\w[-\w /\.]+)`, | ||
const DATASWORN_ID_REGEX = new RegExp( | ||
String.raw`^(?:datasworn:)?${DATASWORN_ID_REGEX_NO_ANCHOR.source}$`, | ||
); | ||
|
||
export const DATASWORN_LINK_REGEX = new RegExp( | ||
String.raw`\[(?<label>[^\]]*)\]\((?<uri>${DATASWORN_ID_REGEX_NO_ANCHOR.source})\)`, | ||
const DATASWORN_LINK_REGEX = new RegExp( | ||
String.raw`\[(?<label>[^\]]*)\]\((?:datasworn:)?(?<id>${DATASWORN_ID_REGEX_NO_ANCHOR.source})\)`, | ||
); | ||
|
||
export type ParsedDataswornId = { | ||
id: string; | ||
kind: string; | ||
path: string; | ||
}; | ||
|
||
/** Given a datasworn URI (e.g., in an href), extracts the kind and the path. | ||
* Works with both old-style invalid URIs (w/o `datasworn:` prefix) and newer `datasworn:` prefixed ones | ||
*/ | ||
export function extractDataswornLinkParts( | ||
uri: string, | ||
): [string, string] | null { | ||
): ParsedDataswornId | null { | ||
const result = uri.match(DATASWORN_ID_REGEX); | ||
return result && [result[1], result[2]]; | ||
return ( | ||
result && { | ||
id: `${result[1]}:${result[2]}`, | ||
kind: result[1], | ||
path: result[2], | ||
} | ||
); | ||
} | ||
|
||
/** Finds a markdown datasworn link and returns the text and URI. | ||
* Works with both old-style invalid URIs (w/o `datasworn:` prefix) and newer `datasworn:` prefixed ones. | ||
*/ | ||
export function matchDataswornLink( | ||
text: string, | ||
): { label: string; id: string } | null { | ||
const match = text.match(DATASWORN_LINK_REGEX); | ||
if (!match) return null; | ||
return { label: match.groups!.label, id: match.groups!.id }; | ||
} | ||
|
||
/** Render a markdown link for a given datasworn ID. */ | ||
export function createDataswornMarkdownLink(label: string, id: string): string { | ||
if (id.startsWith("datasworn:")) | ||
throw new Error( | ||
`Unexpected 'datasworn:' prefix when generating link for '${id}'`, | ||
); | ||
return `[${label}](datasworn:${id})`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters