Skip to content

Commit e679c39

Browse files
committed
misc on inline ref guide, style guide, how ref gen works, add two json examples (folder)
add note about PR 1121 - "broken p5.* links" question on jsdoc best practices doc started a section on how reference-generation works this likely should go in a separate document add example json from reference generation (+new folder) doc style guide: update and link to inline reference guide correct the final "Assets" section clarify that @module and @submodule are for documentation purposes mention @submodule and @for missing from jsdoc
1 parent f698b05 commit e679c39

4 files changed

Lines changed: 431 additions & 31 deletions

File tree

contributor_docs/contributing_to_the_p5js_reference.md

Lines changed: 176 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ In p5.js, we author the code reference you see on the [reference](https://p5js.o
1010

1111
This document will show you how to write and format the reference comments so that they can eventually be rendered onto the website correctly. You should follow this guide whenever you are editing or writing a reference for any p5.js function or variable.
1212

13-
### Aside: A note about versions
13+
### A note about versions
1414

15-
This document primarily describes p5 version 2.x, whose reference documentation is currently being hosted at https://beta.p5js.org/reference/. Important differences between how p5 v1.x and v2.x are documented will be mentioned in order to help the reader who encounters examples of both formats - or tooling - in the wild.
15+
This document describes p5.js version 2.x, whose reference documentation is currently being hosted at https://beta.p5js.org/reference/. Important differences between how p5 v1.x and v2.x are documented will be mentioned in order to help the reader who encounters examples of both formats - or tooling - in the wild.
1616

1717
## A quick introduction to how reference comments work
1818

@@ -112,7 +112,7 @@ Note that block comments starting with `/*` rather than `/**` will be _ignored_
112112

113113
### Differences between p5 v1 and v2
114114

115-
Note that p5 v1.x does _not_ use JSDoc syntax but a slightly modified [syntax called YUIDoc](https://yui.github.io/yuidoc/syntax/index.html). In practice the syntaxes are largely compatible but there _are_ differences.
115+
Note that p5 v1.x does _not_ use JSDoc syntax but a slightly modified [syntax called YUIDoc](https://yui.github.io/yuidoc/syntax/index.html). In practice the syntaxes are largely compatible but there are some differences. This document will highlight any important ones. See also the list in [this appendix](#appendix-syntax-differences-between-yuidoc-and-jsdoc").
116116

117117
Syntax and tooling per version
118118

@@ -692,12 +692,39 @@ p5.prototype._start = function () {
692692
693693
### `@module` and related tags
694694
695-
At the top of each source code file will be a `@module` tag. Modules correspond to a group of features in p5.js which on the rendered reference page on the website are split into the corresponding sections. Inside each module, there are additional submodules defined with the `@submodule` tag.
695+
#### @module and @submodule
696+
697+
At the top of each source code file will be a comment block with a `@module` tag. A module is a top-level grouping of features in the reference pages on the website. This does _not_ necessarily correspond to any specific software `module` concept in the code iteself.
698+
699+
After the `@module` tag, an optional `@submodule` tag can be used to further group features for the website. This `@submodule` tag can be set differently on any comment block in the file to change the submodule for a specific feature.
700+
701+
The convention p5.js follows is that each subfolder in the `src/` folder will be one `@module` while each file inside the subfolder will be its own `@submodule` under the overall subfolder’s `@module`. Unless you are adding new subfolders/files to the p5.js source code, you shouldn’t need to edit a file's top-level reference comment block.
702+
703+
On the website, the [top-level reference page](https://beta.p5js.org/reference) presents a list functions and variables grouped by their modules and submodules.
704+
705+
Examples:
706+
707+
The ["Image" module](https://beta.p5js.org/reference/#Image) in `src/image` has the following submodules: "Loading & Displaying", "Pixels", and "p5.Image".
708+
709+
The ["Color" module](https://beta.p5js.org/reference/#Color) in `src/color` has the following submodules:
710+
"Creating & Reading", "Setting", and "Color Conversion".
711+
712+
The _conceptual_ ["3D" module](https://beta.p5js.org/reference/#3D) documents code mostly from `src/webgl` (there is no `src/3D`), and has the following submodules: "Camera", "Interaction", "Lights", "Material", "strands", "p5.Camera", "p5.Shader".
713+
714+
#### The @for tag
696715
697716
The `@for` tag defines the relationship between this module and the overall `p5` class, effectively saying this module is a part of the `p5` class.
698717
718+
<!-- TODO: clarify the nature of this relationship documented by the @for tag. Where might this be used - still only in docs? also in type-gen? elsewhere? -->
719+
720+
#### The @requires tag
721+
699722
The `@requires` tag defines the required imported modules that the current module depends on.
700723
724+
<!-- TODO: clarify where this @requires info is made use of. What responsibilities does the author have to state these correctly and comprehensively? -->
725+
726+
Example of `@for` and `@requires`
727+
701728
```js
702729
/**
703730
* @module Color
@@ -708,8 +735,6 @@ The `@requires` tag defines the required imported modules that the current modul
708735
*/
709736
```
710737
711-
The convention p5.js follows is that each subfolder in the `src/` folder will be one `@module` while each file inside the subfolder will be its own `@submodule` under the overall subfolder’s `@module`. Unless you are adding new subfolders/files to the p5.js source code, you shouldn’t need to edit this reference comments block.
712-
713738
714739
### `@class` tag
715740
@@ -813,6 +838,10 @@ npm run custom:cleanup
813838
814839
* YUIDoc requires function names be provided with the `@method` tag, whereas JSDoc can generally extract that information from the implementation source code.
815840
841+
* JSDoc does not have either `@submodule` or `@for` tags (YUIDoc does) but we continue to use them throughout the v2.x codebase as a mechanism by which we group functions, variables, and classes on the website. If you run `documentation lint` you'll see complaints about these tag but Documentation.js collects the tagged information into its output without problem.
842+
843+
* YUIDoc code examples (tagged with @example) are automatically wrapped with `<span>` and other markup. For documentation.js, we write the wrapping `<div>` and `<code>` elements around the example code ourselves.
844+
816845
## Appendix: Summary of other documentation differences between p5 v1 and v2
817846

818847
For the syntactic differences between YUIDoc and JSDoc, see above. Here are some other differences:
@@ -827,15 +856,16 @@ For the syntactic differences between YUIDoc and JSDoc, see above. Here are som
827856
828857
## Appendix: About the reference-generation process
829858
830-
In the p5.js repo, `npm run docs` runs (roughly):
831-
```bash
832-
"documentation build ./src/**/*.js
833-
-o ./docs/data.json
834-
&& node ./utils/convert.mjs",
835-
```
836-
### Flow diagram for reference generation
859+
<!-- TODO: move this out to a separate linked file -->
860+
861+
You don't need to know _any_ of the following to successfully write and maintain the reference comments in the p5.js library!
862+
863+
Here's how things work in Feb 2026 - be wary, this description may well be out of date!
864+
865+
//TODO: attach a label/caption. The following prose is essentially the correct screenreader text.
866+
867+
#### Flow diagram for reference generation
837868
838-
See [mermaid flowchart syntax reference](https://mermaid.ai/open-source/syntax/flowchart.html)
839869
840870
```mermaid
841871
graph LR
@@ -857,10 +887,115 @@ graph LR
857887
end
858888
859889
```
860-
### Flow diagram for search index generation
890+
891+
#### reference generation explained
892+
893+
Either a human or Google Actions (CI) executes this command from the p5.js repo:
894+
```bash
895+
npm run docs
896+
```
897+
You can run it locally and observe the outputs.
898+
899+
* TODO: what should be true once this command finishes?
900+
* TODO: when does this command get run?
901+
902+
In turn this command runs:
903+
904+
```bash
905+
"documentation build ./src/**/*.js
906+
-o ./docs/data.json
907+
&& node ./utils/convert.mjs",
908+
```
909+
910+
This is actually two commands, first `documentation build ...`, and then - if the first command was successful `node ./utils/convert.mjs`. We'll look at these in turn.
911+
912+
#### The `documentation build...` command
913+
914+
In summary this command turns JSDoc comment blocks across all the javascript files into a single structured json file ready for further processing.
915+
916+
In more detail:
917+
918+
`documentation build` is the [standard way](https://github.com/documentationjs/documentation?tab=readme-ov-file) to invoke documentation.js to build docs.
919+
920+
* The `-o` is used to specify the output file.
921+
* `documentation.js`'s default output format is JSON, so JSON content is written into the output file.
922+
923+
It's important to note this output does not generate HTML pages, even though that's a common use for tools like documentation.js.
924+
925+
You can run this command yourself locally and inspect the output file to see the gathered data.
926+
927+
You can also run `npx documentation build --help` to read the manual page for documentation.js.
928+
929+
#### The `node ./utils/convert.mjs` command
930+
931+
The second command is
932+
```bash
933+
node ./utils/convert.mjs
934+
```
935+
936+
This will _only_ run if the documentation build command runs successfully.
937+
938+
The `./utils/convert.mjs` script reads the output file generated by the `documentation build` command (the JSON file `/docs/data.json`), and creates three new output files:
939+
* `/docs/reference/data.json` - Used by the p5.js-website to generate the reference pages
940+
* `/docs/reference/data.min.json` - A smaller version of the above
941+
* `/docs/parameterData.json`
942+
943+
The file generated initially by `documentation build` is _very_ large with a lot of data our pages won't need. Further, pieces of information are also scattered over the file - we'd like to group them together into a structure that will make it easier to use in later webpage generation.
944+
The convert script has taken only what's necessary and grouped it together into the new data file `/docs/reference/data.json` and its minified version.
945+
946+
This file will be used by the p5.js-website to generate the final pages, and by another build process on p5.js repo to build the types.
947+
948+
TODO: link to type-generation process description elsewhere.
949+
950+
It has also generated /docs/parameterData.json` which is used by the friendly error system (FES) at runtime to validate function parameters.
951+
952+
#### Excerpt of /docs/reference.data.json
953+
954+
Here's an excerpt from `/docs/reference/data.json`, showing how our `sin` function documentation is now represented at the end of `npm run docs`
955+
956+
```json
957+
{
958+
"name": "sin",
959+
"file": "src/math/trigonometry.js",
960+
"line": 526,
961+
"itemtype": "method",
962+
"description": "<p>Calculates the sine of an angle.</p>\n<p><code>sin()</code> is useful for many geometric tasks in creative coding. The values\nreturned oscillate between -1 and 1 as the input angle increases. <code>sin()</code>\ncalculates the sine of an angle, using radians by default, or according to\nif <a href=\"#/p5/angleMode\">angleMode()</a> setting (RADIANS or DEGREES).</p>\n",
963+
"example": [
964+
"<div>\n<code>\nfunction setup() {\n createCanvas(100, 100);\n\n describe('A white ball on a string oscillates up and down.');\n}\n\nfunction draw() {\n background(200);\n\n // Calculate the coordinates.\n let x = 50;\n let y = 30 * sin(frameCount * 0.05) + 50;\n\n // Draw the oscillator.\n line(50, y, x, y);\n circle(x, y, 20);\n}\n</code>\n</div>\n\n<div>\n<code>\nfunction setup() {\n createCanvas(100, 100);\n\n background(200);\n\n describe('A series of black dots form a wave pattern.');\n}\n\nfunction draw() {\n // Calculate the coordinates.\n let x = frameCount;\n let y = 30 * sin(x * 0.1) + 50;\n\n // Draw the point.\n point(x, y);\n}\n</code>\n</div>\n\n<div>\n<code>\nfunction setup() {\n createCanvas(100, 100);\n\n background(200);\n\n describe('A series of black dots form an infinity symbol.');\n}\n\nfunction draw() {\n // Calculate the coordinates.\n let x = 30 * cos(frameCount * 0.1) + 50;\n let y = 10 * sin(frameCount * 0.2) + 50;\n\n // Draw the point.\n point(x, y);\n}\n</code>\n</div>"
965+
],
966+
"overloads": [
967+
{
968+
"params": [
969+
{
970+
"name": "angle",
971+
"description": "the angle, in radians by default, or according to if <a href=\"/reference/p5/angleMode/\">angleMode()</a> setting (RADIANS or DEGREES).",
972+
"type": "Number"
973+
}
974+
],
975+
"return": {
976+
"description": "sine of the angle.",
977+
"type": "Number"
978+
}
979+
}
980+
],
981+
"return": {
982+
"description": "sine of the angle.",
983+
"type": "Number"
984+
},
985+
"class": "p5",
986+
"static": false,
987+
"module": "Math",
988+
"submodule": "Trigonometry"
989+
},
990+
```
991+
992+
## Appendix: Flow diagram for search index generation
861993
862994
On the p5js-website repo, we run `npm run build:search`
863995
996+
997+
See [mermaid flowchart syntax reference](https://mermaid.ai/open-source/syntax/flowchart.html)
998+
864999
```mermaid
8651000
graph LR
8661001
contentReference["/content/reference"]@{shape: docs}
@@ -873,8 +1008,8 @@ graph LR
8731008
contentExamples --> genSearchIndexForExamples
8741009
buildSearchIndices --> saveSearchIndex[[saveSearchIndex]] --> outDir[[public/search-indices en.json, ja.json, ...]]@{shape: docs}
8751010
```
876-
### Type-generation
877-
1011+
## Appendix: Type-generation
1012+
<!-- TODO - in a separate document -->
8781013
On the p5js repo...
8791014
8801015
`npm run generate-types` runs:
@@ -885,6 +1020,8 @@ On the p5js repo...
8851020
8861021
## TODO:
8871022
* move this TODO section out before submitting PR
1023+
* overhaul the style guide. shared material?
1024+
* where should assets instructions live (where to put store assets and how to refer to them)? in the [documentation style guide](./documentation_style_guide.md) or here?
8881025
* Finish and place the flow diagrams for doc-gen. ?where?
8891026
* Replace first example in Adding examples - it doesn't run no setup or draw. allowed but v unusual in the repo.
8901027
* perhaps acknowledge that there are limits to the types info we can carry in JSDoc and that types are currently sometimes patched in patch.mjs.
@@ -895,19 +1032,35 @@ On the p5js repo...
8951032
* check if tagging incomplete snippets in the markdown as js is harmless (e.g. doesn't break the highlighter library). It provides useful best-effort syntax-highlighting in some contexts, helping to spot errors.
8961033
* ? run a comprehensive broken-link check? broken example check? develop one?
8971034
* add a ToC? there's one on the side panel but it doesn't give a good overview.
1035+
* document the @beta tag (used in e.g. p5.strands)
8981036
8991037
## TODO: Questions to ask
1038+
* Opinions on keeping [JSDoc best practices](https://beta.p5js.org/contribute/jsdoc/) separate? merging it?
1039+
1040+
* Check what this is doing at the top of image.js:
1041+
```
1042+
* @module Image
1043+
* @submodule Image
1044+
```
1045+
* is it attempting to create a submodule to be presented as a sibling of the other submodules?
1046+
* saying the features should be categorised without a distinct submodule? (for that, it's possible to omit the submodule tag)
1047+
1048+
9001049
* What's the url-forming rule for in-documentation linking?
9011050
```
9021051
`#/p5/circle` goes to `p5/circle` but
9031052
`#/p5.Vector` goes to `p5/p5.Vector`
9041053
??? goes to `p5.Vector/setMag`
9051054
??? goes to `p5.Vector/fromAngle`
9061055

1056+
Also see ["#1121 Fix broken p5.* reference links in reference pages"](https://github.com/processing/p5.js-website/pull/1121).
1057+
9071058
```
9081059
* The existing guide shows how to write examples with no setup or draw fn, which are auto-wrapped in setup. Codebase doesn't use this. Downplay / remove the mention of this mechanism?
9091060
* Accept this? "Generally, each example should be a self-contained complete sketch that will run on the reference website and which could be run directly if pasted into the web editor (for example)."
910-
* Accept the following?: Multiple examples are not always separated by @example in the repo. JSDoc says they should be, so i've said that. The guide previously said just put a blank line and then new codeblock.
1061+
* Accept the following?: Multiple examples are not always separated by @example in the repo. JSDoc says they should be, so i've said that. The guide previously said just put a blank line and then new codeblock. Examples where used: ellipse in src/shape/2d_primitives.js
1062+
* regex to find interleave @example tag use `+\* *<\/div>\n *\* *\n *\* *@example`
1063+
9111064
* assets:
9121065
* ? say anything about licensing (e.g. for assets)
9131066
* ? say anything about preferred file formats (for size, for browser compat, for licensing?)
@@ -923,3 +1076,8 @@ On the p5js repo...
9231076
9241077
## Resolved questions
9251078
* Whether to recommend omitting @method tag or not? I'm assuming no: its presence reduces ambiguity and helps codebase search.
1079+
* Confirm that @module and @submodule are constructs for documentation organisation, and don't necessarily indicate the existence of actual software modules of the same name. (I'm happy that's so)
1080+
1081+
### Author appendix: useful tools
1082+
1083+
* grep for modules that don't have submodules: `@module \w+\n +\* +@[^s]`

contributor_docs/documentation_style_guide.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Our community is large and diverse. Many people learn to code using p5.js, and a
1515
## Table of Contents
1616

1717
### Writing
18-
- [documentation.js and JSDoc](#documentation.js-and-jsdoc)
18+
- [documentation.js and JSDoc](#documentationjs-and-jsdoc)
1919
- [English](#english)
2020
- [Oxford Comma](#oxford-comma)
2121
- [Wording](#wording)
@@ -44,11 +44,11 @@ Our community is large and diverse. Many people learn to code using p5.js, and a
4444

4545
## documentation.js and JSDoc
4646

47-
We use [documentation.js](https://documentation.js.org/) to generate the p5.js API documentation.
47+
We use [documentation.js](https://documentation.js.org/) to generate the p5.js API documentation from [JSDoc](https://jsdoc.app/) comments in the p5.js source code.
4848

49-
TODO: add or link to instructions for generating and previewing the reference docs.
49+
Refer to the [inline documentation guide](./contributing_to_the_p5js_reference.md) for more information on how to structure the documentation comments and what tags to use.
5050

51-
Refer to the [inline documentation guide](./contributing_to_the_p5js_reference.md) for more information.
51+
It also discusses how to generate and preview the reference documentation to see your changes.
5252

5353
**[⬆ back to top](#table-of-contents)**
5454

@@ -1265,25 +1265,30 @@ class Mover {
12651265

12661266
## Assets
12671267

1268-
- Always load assets from a folder called "assets".
1268+
Whether assets (such as images, fonts, sound files, etc) are being used in the descriptions or code examples of the reference documentation, or in tutorials, the same rules apply:
12691269

1270-
> Why? It models good project organization. It's also required for assets to load on the p5.js website. Place assets in the following folders to include them in our online documentation:
1271-
- Examples: [src/data/examples/assets](https://github.com/processing/p5.js-website/tree/main/src/data/examples)
1272-
- Reference Pages: [src/templates/pages/reference/assets](https://github.com/processing/p5.js-website/tree/main/src/templates/pages/reference/assets)
1273-
- Learn Pages: [src/assets/learn](https://github.com/processing/p5.js-website/tree/main/src/assets/learn)
1270+
- Always load assets from a folder called `assets`.
1271+
- Store the assets in the `public/assets` folder of the p5.js-website repository.
1272+
1273+
> Why? It models good project organization. It's also required for assets to load on the p5.js website.
12741274
12751275
```javascript
12761276
let img;
12771277

12781278
// Bad.
1279-
function preload() {
1280-
img = loadImage('moonwalk.jpg');
1279+
async function setup() {
1280+
img = await loadImage('moonwalk.jpg');
1281+
//...
12811282
}
12821283

12831284
// Good.
1284-
function preload() {
1285-
img = loadImage('assets/moonwalk.jpg');
1285+
async function setup() {
1286+
img = await loadImage('assets/moonwalk.jpg');
1287+
//...
12861288
}
12871289
```
12881290

1291+
There is more on working with assets in the guide: [Contributing to the p5.js reference](./contributing_to_the_p5js_reference#assets-in-examples).
1292+
1293+
12891294
**[⬆ back to top](#table-of-contents)**

0 commit comments

Comments
 (0)