Page MenuHomePhabricator

docs: Provide code samples that work in MediaWiki
Closed, ResolvedPublic5 Estimated Story Points

Assigned To
Authored By
Catrope
Dec 3 2022, 12:29 AM
Referenced Files
F36869820: image.png
Feb 24 2023, 10:03 PM
F36869816: image.png
Feb 24 2023, 10:03 PM
F36867483: image.png
Feb 24 2023, 10:03 PM
F36867481: image.png
Feb 24 2023, 10:03 PM
Tokens
"Yellow Medal" token, awarded by Volker_E.

Description

A lot of Codex users (including all users using it inside MediaWiki) don't use TypeScript. However, all code examples in the documentation do use TypeScript. This is confusing for people who don't know TypeScript and don't know how to mentally transform TypeScript code to plain JavaScript code. We should provide code examples that are readable and copy-pasteable for the majority of our users.

There are three main approaches we could take:

  1. Provide both TS and plain JS versions of the same examples. Allow viewers of the docs site to toggle between them
  2. Change all our examples from TS to plain JS, leaving no TS examples
  3. Change all our examples from TS to JS with type annotation comments

#1 would be ideal, but would take more work. In the short term, #2 would be easier to achieve. I'm not sure that #3 has as much value, because it's not very easy to mentally transform JS with jsdoc comments to TypeScript either.

Additionally, there are other barriers to copy-pasting code from our docs site into MediaWiki that we should consider:

  • import/export vs require/module.exports
  • Self-closing tag treatment: <cdx-icon :icon="foo" /> vs <cdx-icon :icon="foo"></cdx-icon>
  • Importing tokens (when using mixins, see also T328602): @import '@wikimedia/codex-design-tokens/theme-wikimedia-ui.less'; vs @import 'mediawiki.skin.variables.less';

For those reasons we should consider providing MW-specific code samples alongside generic code samples where the two differ.

Approach

We decided to:

  • Make the gap between our code samples and code that works in MW smaller:
    • Change all code samples to use plain JS (with type annotation comments where useful)
    • Only use ES6 syntax in code samples, to improve MediaWiki compatibility
    • Remove getEventLogger, and replace it with hard-coded event handlers in each example
  • Automatically generate MW-compatible versions of the code samples
    • Write a script that applies transformations to a regular code example to make it MW-compatible
      • Change import/export to require/module.exports
      • Change imports from @wikimedia/codex-icons to import from ./icons.json instead
      • Change Less imports of @wikimedia/codex-design-tokens to import mediawiki.skin.variables.less instead
      • Change self-closing tags to non-self-closing tags
    • Build the generated code samples to an examples-mw directory (packages/codex-docs/component-demos/COMPONENT-NAME/examples-mw/) and gitignore those directories
    • Update all the .md files to use a code group with two tabs, like this

Acceptance Criteria

  • Our code samples don't use TypeScript or ES >6 syntax
  • All code samples at https://doc.wikimedia.org/codex are available in regular and MW-targeted versions

Event Timeline

Restricted Application added a subscriber: Aklapper. · View Herald Transcript

@Catrope - can you please put an initial/proposed priority level on this task and move it to the Backlog column?
DST prioritization guidance

Catrope renamed this task from Provide non-TypeScript code samples in demos to Provide code samples that work in MediaWiki.Feb 22 2023, 1:00 AM
Catrope triaged this task as Medium priority.
Catrope updated the task description. (Show Details)
Catrope moved this task from Inbox to Backlog on the Design-System-Team board.
Volker_E renamed this task from Provide code samples that work in MediaWiki to docs: Provide code samples that work in MediaWiki.Feb 23 2023, 11:10 AM
Volker_E added a project: Documentation.
Volker_E subscribed.

Related T330303

We could use the code groups feature in VitePress to build something like this:

image.png (806×755 px, 61 KB)
image.png (806×755 px, 60 KB)

Or we could use the Codex tabs component to do something similar:

image.png (777×753 px, 61 KB)
image.png (777×753 px, 60 KB)

Oh cool, the code groups feature is exactly what we need IMO! Looks great; much better than the Codex tabs.

That's really nice. ✨ I'd leave the tabs as is, the dark mode makes them clearer combined about what the tabs are actually about. The only small amendment I'd make is using the same 2px bottom border.

Change 905593 had a related patch set uploaded (by Catrope; author: Catrope):

[design/codex@main] [proof of concept] Add MediaWiki versions of all code samples

https://gerrit.wikimedia.org/r/905593

Are we concerned that this is going to add a documentation / maintenance burden to component development in Codex? I would say there is already a pretty high documentation-to-shipping-code ratio in this project, and this might increase it further.

What about a single top-level page about using Codex in MediaWiki that uses this "code groups" feature for now? Having to write and maintain 2x the amount of demos for every component seems daunting to me, especially when most of the changes are trivial (use require instead of import, no self-closing component tags, etc). Even our use of TS syntax is pretty limited in the examples I would say.

The proof of concept looks great! I love that we can just use the built-in tabs in the code window. I think having MediaWiki-specific code samples will be incredibly valuable to our users, some of whom have already asked for this feature.

I agree with @egardner that we should try to avoid increasing the documentation burden on developers. For that reason, I think we should:

  • Only use as-is generated code samples, like the ones @Catrope set up in the proof of concept. We might be tempted to generate code samples and then doctor them up to add things like extra documentation (since we're losing the TypeScript annotations), but I don't think that's worth the extra work it would mean
  • If possible, not commit these generated files, but instead generate them as part of the docs build process

If we do these things, we will not be adding any additional burden to Codex developers.

Would there be an expectation to generate these MW-flavored code samples every time a new demo was added to the docs? Is there any chance this process will fail based on new syntax that has not been encountered yet? These are still concerns in my opinion.

I don't want developers to have to potentially troubleshoot this process in the middle of an unrelated patch.

Restricted Application raised the priority of this task from Medium to High. · View Herald TranscriptApr 10 2023, 8:51 PM

Per today's DST engineering meeting, we're going to try to get some additional feedback from Codex users about how important this feature is (using the deploy preview of @Catrope's patch).

If folks think that this is worth doing, we should also consider adding some way to exclude individual code samples from this script that auto-converts them, in case certain snippets end up being extra troublesome for whatever reason.

CCiufo-WMF lowered the priority of this task from High to Medium.Jun 28 2023, 3:34 PM
CCiufo-WMF set the point value for this task to 5.
CCiufo-WMF moved this task from Needs Refinement to Up Next on the Design-System-Team board.

Change 939388 had a related patch set uploaded (by Catrope; author: Catrope):

[design/codex@main] docs: Limit code examples to plain JS (no TypeScript) and ES6

https://gerrit.wikimedia.org/r/939388

Change 940270 had a related patch set uploaded (by Catrope; author: Catrope):

[design/codex@main] docs: Generate MediaWiki-targeted versions of every code example

https://gerrit.wikimedia.org/r/940270

Change 905593 abandoned by Catrope:

[design/codex@main] [proof of concept] Add MediaWiki versions of all code samples

Reason:

Superseded by https://gerrit.wikimedia.org/r/c/design/codex/+/940270/

https://gerrit.wikimedia.org/r/905593

Change 939388 merged by jenkins-bot:

[design/codex@main] docs: Limit code examples to plain JS (no TypeScript) and ES6

https://gerrit.wikimedia.org/r/939388

Change 940270 merged by jenkins-bot:

[design/codex@main] docs, build: Generate MediaWiki-targeted versions of every code example

https://gerrit.wikimedia.org/r/940270

Change 940941 had a related patch set uploaded (by Anne Tomasevich; author: Anne Tomasevich):

[design/codex@main] docs: Remove margin and border-radius above code groups

https://gerrit.wikimedia.org/r/940941

Change 940941 merged by jenkins-bot:

[design/codex@main] docs: Remove margin and border-radius above code groups

https://gerrit.wikimedia.org/r/940941

Change 944353 had a related patch set uploaded (by Catrope; author: Catrope):

[mediawiki/core@master] Update Codex from v0.15.0 to v0.16.1

https://gerrit.wikimedia.org/r/944353

Change 944353 merged by jenkins-bot:

[mediawiki/core@master] Update Codex from v0.15.0 to v0.16.1

https://gerrit.wikimedia.org/r/944353

Volker_E assigned this task to Catrope.
Volker_E updated the task description. (Show Details)