Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

👩‍⚕️ Add a {doc} role to support sphinx #387

Merged
merged 1 commit into from
May 15, 2023
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
5 changes: 5 additions & 0 deletions .changeset/modern-ants-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'myst-roles': patch
---

Add the {doc} role to look to other documents
30 changes: 30 additions & 0 deletions packages/myst-roles/src/doc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { RoleSpec } from 'myst-common';
import { fileWarn, ParseTypesEnum } from 'myst-common';

const REF_PATTERN = /^(.+?)<([^<>]+)>$/; // e.g. 'Labeled Document <doc>'

export const docRole: RoleSpec = {
name: 'doc',
body: {
type: ParseTypesEnum.string,
required: true,
},
run(data, vfile) {
const body = data.body as string;
const match = REF_PATTERN.exec(body);
const [, modified, rawLabel] = match ?? [];
const url = rawLabel ?? body;
fileWarn(
vfile,
'Usage of the {doc} role is not recommended, use a markdown link to the file instead.',
{ source: 'role:doc', note: `For {doc}\`${body}\` use [${modified || ''}](${url})` },
);
return [
{
type: 'link',
url,
children: modified ? [{ type: 'text', value: modified.trim() }] : undefined,
},
];
},
};
3 changes: 3 additions & 0 deletions packages/myst-roles/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { citeRole } from './cite';
import { deleteRole } from './delete';
import { mathRole } from './math';
import { refRole } from './reference';
import { docRole } from './doc';
import { siRole } from './si';
import { evalRole } from './inlineExpression';
import { smallcapsRole } from './smallcaps';
Expand All @@ -18,6 +19,7 @@ export const defaultRoles = [
deleteRole,
mathRole,
refRole,
docRole,
siRole,
evalRole,
smallcapsRole,
Expand All @@ -31,6 +33,7 @@ export { citeRole } from './cite';
export { deleteRole } from './delete';
export { mathRole } from './math';
export { refRole } from './reference';
export { docRole } from './doc';
export { siRole } from './si';
export { smallcapsRole } from './smallcaps';
export { subscriptRole } from './subscript';
Expand Down