Skip to content

Commit 2cbb184

Browse files
committed
✨ NEW: Add path and project link schemes
Part of implementing jupyter-book/myst-enhancement-proposals#10
1 parent 022d397 commit 2cbb184

File tree

14 files changed

+260
-162
lines changed

14 files changed

+260
-162
lines changed

docs/faq/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ The [MyST-NB](https://myst-nb.readthedocs.io) tool provides a Sphinx extension f
5454
### Include a file from outside the docs folder (like README.md)
5555

5656
:::{seealso}
57-
[...](#organising-content/include)
57+
<project:#organising-content/include>
5858
:::
5959

6060
You can include a file, including one from outside the project using e.g.:
@@ -118,7 +118,7 @@ See [](#syntax/implicit-targets) for this information.
118118

119119
### Suppress warnings
120120

121-
See [...](#myst-warnings) for this information.
121+
See <project:#myst-warnings> for this information.
122122

123123
### Sphinx-specific page front matter
124124

docs/intro.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ There are a range of great HTML themes that work well with MyST, such as [sphinx
107107

108108
## Configuring MyST-Parser
109109

110-
The [...](configuration.md) section contains a complete list of configuration options for the MyST-Parser.
110+
The <project:configuration.md> section contains a complete list of configuration options for the MyST-Parser.
111111

112112
These can be applied globally, e.g. in the sphinx `conf.py`:
113113

docs/syntax/code_and_apis.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ Sphinx and MyST provide means to analyse source code and automatically generate
170170

171171
As opposed to `sphinx.ext.autodoc`, `sphinx-autodoc2` performs static (rather than dynamic) analysis of the source code, integrates full package documenting, and also allows for docstrings to be written in both RestructureText and MyST.
172172

173-
The `auto_mode` will automatically generate the full API documentation, as shown [...](/apidocs/index.rst).
173+
The `auto_mode` will automatically generate the full API documentation, as shown <project:/apidocs/index.rst>.
174174

175175
Alternatively, the `autodoc2-object` directive can be used to generate documentation for a single object.
176176
To embed in a MyST document the MyST `render_plugin` should be specified, for example:

docs/syntax/cross-referencing.md

Lines changed: 69 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ For example, using `myst_heading_anchors = 2`:
7878

7979
## A heading with slug
8080

81-
[...](#a-heading-with-slug)
81+
<project:#a-heading-with-slug>
8282

8383
[Explicit title](#a-heading-with-slug-1)
8484
::::
@@ -136,74 +136,84 @@ or an **internal** target, such as a file, heading or figure within the same pro
136136

137137
By default, MyST will resolve link destinations according to the following rules:
138138

139-
1. Destinations beginning with `inv:` will be treated as links to intersphinx references ([see below](#syntax/inv_links)).
139+
1. Destination beginning with a scheme (e.g. `xxx:`), will be handled according to that scheme:
140140

141-
2. Autolinks or destinations beginning with `http:`, `https:`, `ftp:`, or `mailto:` will be treated as external [URL] links.
141+
{style=lower-roman}
142+
1. Destinations beginning with `project:` will be treated as internal references
143+
2. Destinations beginning with `path:` will be treated as downloadable files
144+
3. Destinations beginning with `inv:` will be treated as intersphinx references
145+
4. Autolinks or destinations beginning with `http:`, `https:`, `ftp:`, or `mailto:` will be treated as external [URL] links.
142146

143-
3. Destinations which point to a local file path are treated as links to that file.
147+
2. Destinations which point to a local file path are treated as links to that file.
144148

145-
- If the destination is a relative path, it is resolved relative to the current file.
146-
- If the destination is an absolute path (starts with `/`), it is resolved relative to the root of the project (i.e. the source directory).
147-
- If that path relates to another document in the project (e.g. a `.md` or `.rst` file), then it will link to the first heading in that document.
148-
- Links to project documents can also include a `#` fragment identifier, to link to a specific heading in that document.
149-
- If the path is to a non-source file (e.g. a `.png` or `.pdf` file), then the link will be to the file itself, e.g. to download it.
149+
{style=lower-roman}
150+
1. If the destination is a relative path, it is resolved relative to the current file.
151+
2. If the destination is an absolute path (starts with `/`), it is resolved relative to the root of the project (i.e. the source directory).
152+
3. If that path relates to another document in the project (e.g. a `.md` or `.rst` file), then it will link to the first heading in that document.
153+
4. Links to project documents can also include a `#` fragment identifier, to link to a specific heading in that document.
154+
5. If the path is to a non-source file (e.g. a `.png` or `.pdf` file), then the link will be to the file itself, e.g. to download it.
150155

151-
4. Destinations beginning with `#` will be treated as internal references.
156+
3. Destinations beginning with `#` will be treated as internal references.
152157

153-
- First, explicit target in the same file are searched for, if not found
154-
- Then, implicit targets in the same file are searched for, if not found
155-
- Then, explicit targets across the whole project are searched for, if not found
156-
- Then, intersphinx references are searched for, if not found
157-
- A warning is emitted and the destination is left as an external link.
158+
{style=lower-roman}
159+
1. First, explicit targets in the same file are searched for, if not found
160+
2. Then, implicit targets in the same file are searched for, if not found
161+
3. Then, explicit targets across the whole project are searched for, if not found
162+
4. Then, intersphinx references are searched for, if not found
163+
5. A warning is emitted and the destination is left as an external link.
158164

159165
:::{note}
160166
Local file path resolution and cross-project references are not available in [single page builds](#myst-docutils)
161167
:::
162168

163169
### Explicit vs implicit link text
164170

165-
If the link text is explicitly given, e.g. `[text](dest)`, then the link text will be the given text.
171+
If the link text is explicitly given, e.g. `[text](#dest)`, then the rendered text will be that.
166172
This text can contain nested inline markup, such as `[*emphasis*](#syntax/emphasis)`{l=md}.
167173

168-
If no text or `...` is given, e.g. `[](dest)` or `[...](dest)`, then MyST will attempt to resolve an implicit text.
174+
If no text is given or it is an auto-link, e.g. `[](#dest)` or `<project:#dest>`, then MyST will attempt to resolve an implicit text.
169175
For example, if the destination is a heading, then the heading text will be used as the link text,
170176
or if the destination is a figure/table then the caption will be used as the link text.
171177
Otherwise, the link text will be the destination itself.
172178

173-
### Some examples
179+
### Examples
174180

175-
Here are some examples:
181+
#### Autolinks
176182

177-
:::{list-table}
178-
:header-rows: 1
183+
:::{myst-example}
179184

180-
* - Type
181-
- Syntax
182-
- Rendered
185+
:External URL: <https://example.com>
186+
:Internal target reference: <project:#cross-references>
187+
:Internal file reference: <project:../intro.md>
188+
:Internal file -> heading reference: <project:../intro.md#-get-started>
189+
:Downloadable file: <path:example.txt>
190+
:Intersphinx reference: <inv:sphinx:std#index>
183191

184-
* - Autolink
185-
- `<https://example.com>`
186-
- <https://example.com>
192+
:::
193+
194+
#### Inline links with implicit text
195+
196+
:::{myst-example}
187197

188-
* - External URL
189-
- `[example.com](https://example.com)`
190-
- [example.com](https://example.com)
198+
:External URL: [](https://example.com)
199+
:Internal target reference: [](#cross-references)
200+
:Internal file reference: [](../intro.md)
201+
:Internal file -> heading reference: [](../intro.md#-get-started)
202+
:Downloadable file: [](example.txt)
203+
:Intersphinx reference: [](inv:sphinx:std#index)
191204

192-
* - Document file
193-
- `[Source file](../intro.md)`
194-
- [Source file](../intro.md)
205+
:::
195206

196-
* - Non-document file
197-
- `[Non-source file](example.txt)`
198-
- [Non-source file](example.txt)
207+
#### Inline links with explicit text
199208

200-
* - Local heading
201-
- `[...](#cross-references)`
202-
- [...](#cross-references)
209+
:::{myst-example}
203210

204-
* - Heading in other file
205-
- `[Heading](../intro.md#installation)`
206-
- [Heading](../intro.md#installation)
211+
:External URL: [Explicit text](https://example.com)
212+
:Internal target reference: [Explicit text](#cross-references)
213+
:Internal file reference: [Explicit text](../intro.md)
214+
:Internal file -> heading reference: [Explicit text](../intro.md#-get-started)
215+
:Downloadable file: [Explicit text](example.txt)
216+
:Intersphinx reference: [Explicit text](inv:sphinx:std#index)
207217

208218
:::
209219

@@ -341,23 +351,23 @@ Here are some examples:
341351
- Rendered
342352

343353
* - Autolink, full
344-
- `<inv:sphinx:std:doc#index>`
354+
- `<inv:sphinx:std:doc#index>`{l=myst}
345355
- <inv:sphinx:std:doc#index>
346356

347357
* - Link, full
348-
- `[Sphinx](inv:sphinx:std:doc#index)`
358+
- `[Sphinx](inv:sphinx:std:doc#index)`{l=myst}
349359
- [Sphinx](inv:sphinx:std:doc#index)
350360

351361
* - Autolink, no type
352-
- `<inv:sphinx:std#index>`
362+
- `<inv:sphinx:std#index>`{l=myst}
353363
- <inv:sphinx:std#index>
354364

355365
* - Autolink, no domain
356-
- `<inv:sphinx:*:doc#index>`
366+
- `<inv:sphinx:*:doc#index>`{l=myst}
357367
- <inv:sphinx:*:doc#index>
358368

359369
* - Autolink, only name
360-
- `<inv:#*.Sphinx>`
370+
- `<inv:#*.Sphinx>`{l=myst}
361371
- <inv:#*.Sphinx>
362372

363373
:::
@@ -371,21 +381,21 @@ These can also within MyST documents, although it is recommended to use the Mark
371381
:::{myst-example}
372382
- {ref}`syntax/referencing`, {ref}`Explicit text <syntax/referencing>`
373383
- {term}`my other term`
374-
- {doc}`../intro`
375-
- {download}`example.txt`
376-
- {py:class}`mypackage.MyClass`
377-
- {external:class}`sphinx.application.Sphinx`
378-
- {external+sphinx:ref}`code-examples`
384+
- {doc}`../intro`, {doc}`Explicit text <../intro>`
385+
- {download}`example.txt`, {download}`Explicit text <example.txt>`
386+
- {py:class}`mypackage.MyClass`, {py:class}`Explicit text <mypackage.MyClass>`
387+
- {external:class}`sphinx.application.Sphinx`, {external:class}`Explicit text <sphinx.application.Sphinx>`
388+
- {external+sphinx:ref}`code-examples`, {external+sphinx:ref}`Explicit text <code-examples>`
379389

380390
---
381391

382-
- [...][syntax], [Explicit text][syntax]
383-
- [...](<#my other term>)
384-
- [...](../intro.md)
385-
- [...](example.txt)
386-
- [...](#mypackage.MyClass)
387-
- [...](#sphinx.application.Sphinx), [...](inv:#*Sphinx)
388-
- [...](inv:sphinx#code-examples)
392+
- <project:#syntax/referencing>, [][syntax], [Explicit text][syntax]
393+
- [](<#my other term>)
394+
- <project:../intro.md>, [Explicit text](../intro.md)
395+
- <path:example.txt>, [Explicit text](example.txt)
396+
- <project:#mypackage.MyClass>, [Explicit text](#mypackage.MyClass)
397+
- <inv:#*Sphinx>, [Explicit text](#sphinx.application.Sphinx)
398+
- <inv:sphinx#code-examples>, [Explicit text](inv:sphinx#code-examples)
389399

390400
[syntax]: #syntax/referencing
391401
:::

docs/syntax/images_and_figures.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ figclass : text
119119

120120
:::::{seealso}
121121

122-
See the [...](#syntax/md-figures) section for information on how to create figures that use native Markdown images.
122+
See the <project:#syntax/md-figures> section for information on how to create figures that use native Markdown images.
123123

124124
::::{myst-example}
125125
:::{figure-md}

myst_parser/_docs.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ def field_type(field):
107107

108108

109109
class MystConfigDirective(_ConfigBase):
110-
111110
option_spec = {
112111
"sphinx": directives.flag,
113112
"extensions": directives.flag,
@@ -120,7 +119,6 @@ def run(self):
120119
text = self.table_header()
121120
count = 0
122121
for name, value, field in config.as_triple():
123-
124122
if field.metadata.get("deprecated"):
125123
continue
126124

@@ -378,7 +376,7 @@ def run(self):
378376
# TODO when some more work and testing, this should be made available publicly
379377

380378
from pygments import token # noqa: E402
381-
from pygments.lexer import bygroups, inherit # noqa: E402
379+
from pygments.lexer import bygroups, inherit, this, using # noqa: E402
382380
from pygments.lexers.markup import MarkdownLexer # noqa: E402
383381

384382

@@ -412,12 +410,12 @@ class MystLexer(MarkdownLexer):
412410
),
413411
# :name: value
414412
(
415-
r"^(\:)([^\n]+)(\:)([^\n]+)(\n)",
413+
r"^(\:)([^\n\:]+)(\:)([^\n]+)(\n)",
416414
bygroups(
417415
token.Punctuation,
418-
token.Name.Label,
416+
token.Generic.Strong,
419417
token.Punctuation,
420-
token.Text,
418+
using(this, state="inline"),
421419
token.Text,
422420
),
423421
),
@@ -429,7 +427,18 @@ class MystLexer(MarkdownLexer):
429427
# {name}
430428
(
431429
r"(\{)([a-zA-Z0-9+:-]+)(\})",
432-
bygroups(token.Punctuation, token.Name.Label, token.Punctuation),
430+
bygroups(token.Punctuation, token.Operator.Word, token.Punctuation),
431+
),
432+
# <http:example.com>
433+
(
434+
r"(<)(http|https|mailto|project|path|inv)(\:)([^\s>]+)(>)",
435+
bygroups(
436+
token.Punctuation,
437+
token.String.Other,
438+
token.String.Other,
439+
token.Name.Label,
440+
token.Punctuation,
441+
),
433442
),
434443
inherit,
435444
],

0 commit comments

Comments
 (0)