Skip to content

Commit 3331b6b

Browse files
committed
Add documentation for sphinxJsConfig.ts hooks
1 parent 10348d9 commit 3331b6b

2 files changed

Lines changed: 108 additions & 28 deletions

File tree

README.rst

Lines changed: 99 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,6 @@ This directive documents all exports on a module. For example::
364364

365365
.. js:automodule:: package.submodule
366366

367-
368367
autosummary
369368
-----------
370369

@@ -477,6 +476,10 @@ Configuration Reference
477476
one. If there is more than one, ``root_for_relative_js_paths`` must be
478477
specified as well. Defaults to ``../``.
479478

479+
``root_for_relative_js_paths``
480+
Relative JS entity paths are resolved relative to this path. Defaults to
481+
``js_source_path`` if not present.
482+
480483
``jsdoc_config_path``
481484
A conf.py-relative path to a JSDoc config file, which is useful if you want to
482485
specify your own JSDoc options, like recursion and custom filename matching.
@@ -485,15 +488,91 @@ Configuration Reference
485488
``jsdoc_tsconfig_path``
486489
If using TypeDoc, specify the path of ``tsconfig.json`` file
487490

488-
``root_for_relative_js_paths``
489-
Relative JS entity paths are resolved relative to this path. Defaults to
490-
``js_source_path`` if it is only one item.
491+
``ts_type_xref_formatter``
492+
A function for formatting typescript type cross referenes. See the
493+
"Typescript: Cross references" section below.
494+
495+
``ts_type_bold``
496+
Make all typescript types bold if `true`.
497+
498+
``ts_sphinx_js_config``
499+
A link to a typescript config file.
500+
501+
The `ts_sphinx_js_config` file
502+
------------------------------
503+
504+
This file should be a typescript module. It's executed in a context where it can
505+
import `typedoc` and `sphinx_js`. These functions take TypeDoc IR objects as
506+
arguments. Since the TypeDoc IR is unstable, this config may often break when
507+
switching TypeDoc versions. However, these hooks are very powerful so using them
508+
may be worthwhile anyways. This API is experimental and may change in the
509+
future.
510+
511+
For an example, you can see Pyodide's config file `here <shouldDestructureArg>`__.
512+
513+
This file should export a config object with some of the three following
514+
functions::
515+
516+
* ``shouldDestructureArg: (param: ParameterReflection) => boolean``
517+
518+
This function takes a ``ParameterReflection`` and decides if it should be
519+
destructured. If so, it's equivalent to putting a `@destructure` tag for the
520+
argument. For example:
521+
522+
.. code_block:: ts
523+
524+
function shouldDestructureArg(param: ParameterReflection) {
525+
return param.name === "options";
526+
}
527+
528+
* ``preConvert?: (app: Application) => Promise<void>;``
529+
530+
This hook is called with the TypeDoc application as argument before the
531+
typescript files are parsed. For example, it can be used to add extra TypeDoc
532+
plugins.
533+
534+
* ``postConvert: (app: Application, project: ProjectReflection, typeDocToIRMap: Map<DeclarationReflection, TopLevelIR>) => void``
535+
536+
This hook is called after the sphinx_js IR is created. It can be used to
537+
modify the IR arbitrarily. It is very experimental and subject to breaking
538+
changes.
539+
540+
For example, this ``postConvert`` hook removes the constructor from classes marked with
541+
`@hideconstructor`.
542+
543+
.. code_block:: ts
544+
545+
function postConvert(app, project, typeDocToIRMap) {
546+
for (const [key, value] of typeDocToIRMap.entries()) {
547+
if (value.kind === "class" && value.modifier_tags.includes("@hideconstructor")) {
548+
value.constructor_ = null;
549+
}
550+
}
551+
}
552+
553+
To use it, you'll also need to add a tag defintion for `@hideconstructor` to your `tsdoc.json` file:
554+
555+
.. code_block:: json
556+
557+
"tagDefinitions": [
558+
{
559+
"tagName": "@hideconstructor",
560+
"syntaxKind": "modifier"
561+
}
562+
]
563+
564+
This ``postConvert`` hook hides external attributes and functions from the documentation:
565+
566+
.. code_block:: ts
567+
568+
function postConvert(app, project, typeDocToIRMap) {
569+
for (const [key, value] of typeDocToIRMap.entries()) {
570+
if (value.kind === "attribute" || value.kind === "function") {
571+
value.is_private = key.flags.isExternal || key.flags.isPrivate;
572+
}
573+
}
574+
}
491575

492-
``jsdoc_cache``
493-
Path to a file where JSDoc output will be cached. If omitted, JSDoc will be
494-
run every time Sphinx is. If you have a large number of source files, it may
495-
help to configure this value. But be careful: the cache is not automatically
496-
flushed if your source code changes; you must delete it manually.
497576

498577
How sphinx-js finds typedoc / jsdoc
499578
-----------------------------------
@@ -518,26 +597,24 @@ documentation. A particularly juicy page is
518597
`<https://mozilla.github.io/fathom/ruleset.html>`__. Click the "View page
519598
source" link to see the raw directives.
520599

600+
For a typescript example, see `the Pyodide api docs
601+
<https://pyodide.org/en/stable/usage/api/js-api.html>`__.
602+
521603
`ReadTheDocs <https://readthedocs.org/>`__ is the canonical hosting platform for
522-
Sphinx docs and now supports sphinx-js as an opt-in beta. Put this in the
523-
``.readthedocs.yml`` file at the root of your repo::
604+
Sphinx docs and now supports sphinx-js. Put this in the
605+
``.readthedocs.yml`` file at the root of your repo:
524606

525-
requirements_file: docs/requirements.txt
526-
build:
527-
image: latest
607+
.. code_block:: yaml
608+
609+
python:
610+
install:
611+
- requirements: docs/requirements.txt
528612

529613
Then put the version of sphinx-js you want in ``docs/requirements.txt``. For
530614
example::
531615

532616
sphinx-js==3.1.2
533617

534-
Or, if you prefer, the Fathom repo carries a `Travis CI configuration
535-
<https://github.com/mozilla/fathom/blob/92304b8ad4768e90c167c3d93f9865771f5a6d80/.travis.yml#L41>`__
536-
and a `deployment script
537-
<https://github.com/mozilla/fathom/blob/92304b8ad4768e90c167c3d93f9865771f5a6d80/tooling/travis-deploy-docs>`__
538-
for building docs with sphinx-js and publishing them to GitHub Pages. Feel free
539-
to borrow them.
540-
541618
Caveats
542619
=======
543620

@@ -586,7 +663,7 @@ Version History
586663
(pyodide/sphinx-js-fork#47)
587664
* Added support for destructuring the documentation of keyword arguments in
588665
Typescript using the ``@destructure`` tag or the
589-
``ts_should_destructure_arg`` hook.
666+
``shouldDestructureArg`` hook.
590667
(pyodide/sphinx-js-fork#48, pyodide/sphinx-js-fork#74,
591668
pyodide/sphinx-js-fork#75, pyodide/sphinx-js-fork#101,
592669
pyodide/sphinx-js-fork#128)

sphinx_js/__init__.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,23 @@ def setup(app: Sphinx) -> None:
5151
app.add_config_value(
5252
"js_source_path", default=["../"], rebuild="env", types=[str, list]
5353
)
54+
55+
# We could use a callable as the "default" param here, but then we would
56+
# have had to duplicate or build framework around the logic that promotes
57+
# js_source_path to a list and calls abspath() on it. It's simpler this way
58+
# until we need to access js_source_path from more than one place.
59+
app.add_config_value("root_for_relative_js_paths", None, "env")
60+
5461
app.add_config_value("jsdoc_config_path", default=None, rebuild="env")
5562
app.add_config_value("jsdoc_tsconfig_path", default=None, rebuild="env")
63+
5664
app.add_config_value("ts_type_xref_formatter", None, "env")
5765
app.add_config_value("ts_type_bold", False, "env")
5866
app.add_config_value("ts_sphinx_js_config", None, "env")
67+
5968
app.add_css_file(SPHINX_JS_CSS)
6069
app.connect("build-finished", on_build_finished)
6170

62-
# We could use a callable as the "default" param here, but then we would
63-
# have had to duplicate or build framework around the logic that promotes
64-
# js_source_path to a list and calls abspath() on it. It's simpler this way
65-
# until we need to access js_source_path from more than one place.
66-
app.add_config_value("root_for_relative_js_paths", None, "env")
67-
6871

6972
def analyze(app: Sphinx) -> None:
7073
"""Run JSDoc or another analysis tool across a whole codebase, and squirrel

0 commit comments

Comments
 (0)