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

Replace {{anch}} macro with Markdown links #13802

Merged
merged 9 commits into from
Mar 14, 2022
Merged

Replace {{anch}} macro with Markdown links #13802

merged 9 commits into from
Mar 14, 2022

Conversation

queengooborg
Copy link
Collaborator

@queengooborg queengooborg commented Mar 12, 2022

This PR is a continuation off of #13347. This PR replaces all of the calls to the {{arch}} macro with Markdown links, using a simple find-and-replace script. This will allow us to remove the macro from MDN entirely. The script takes into account the second variable passed in as display text as well.

Script used:

import {promises as fs} from "fs";
import path from "path";

// https://gist.github.com/lovasoa/8691344
async function* walk(dir) {
	for await (const d of await fs.opendir(dir)) {
		const entry = path.join(dir, d.name);
		if (d.isDirectory()) yield* walk(entry);
		else if (d.isFile() && d.name != ".DS_Store") yield entry;
	}
}
// END SNIPPET

const re = /([\w"]+>[\w\s]*)?(`)?(?<!\\){{anch\("([^"]*)"(?:, "([^"]*)")?\)}}(?:`)?/g;

const main = async () => {
	for await (const p of walk((`${process.env.HOME}/Developer/Gooborg/mdn-content/files`))) {
		const isHTMLFile = p.endsWith('.html');
		let contents = await fs.readFile(p, 'utf-8');
		let changed = false;

		const matches = [...contents.matchAll(re)];
		for (const match of matches) {
			const isHTML = isHTMLFile || !!match[1];

			let text = match[4] || match[3];
			if (match[2]) text = isHTML ? `<code>${text}</code>` : `\`${text}\``;
			const link = match[3]
				.replace(/\n\//g, '')
				.replace(/[\s\(\)]/g, '_')
				.replace(/_+/g, '_')
				.toLowerCase();
			const anchor = isHTML ? `<a href="#${link}">${text}</a>` : `[${text}](#${link})`;

			changed = true;
			contents = contents.replace(match[0], (match[1] || '') + anchor);
		}

		if (changed) {
			await fs.writeFile(p, contents);
		}
	}
}

await main();

Notice: this is definitely a big PR, and I'm happy to split into smaller chunks if desired.

@queengooborg queengooborg requested a review from a team as a code owner March 12, 2022 11:10
@queengooborg queengooborg requested a review from a team March 12, 2022 11:10
@queengooborg queengooborg requested review from a team as code owners March 12, 2022 11:10
@queengooborg queengooborg requested review from rebloor, sideshowbarker, Elchi3 and estelle and removed request for a team March 12, 2022 11:10
@github-actions github-actions bot added Content:Accessibility Accessibility docs Content:CSS Cascading Style Sheets docs Content:DevTools Firefox DevTools docs (retired) Content:HTML Hypertext Markup Language docs Content:HTTP HTTP docs Content:JS JavaScript docs Content:Learn Learning area docs Content:Media Media docs Content:Other Any docs not covered by another "Content:" label Content:SVG SVG docs Content:wasm WebAssembly docs Content:WebAPI Web API docs labels Mar 12, 2022
Copy link
Collaborator

@wbamberg wbamberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice work! Thanks @queengooborg !

Lots of comments but really only a few categories:

  • usages that need to be HTML links, either because the whole page is HTML (devtools) or because the containing element is an HTML table (including the HTML <input> elements, but some other cases too). Ideally we could convert these usages into HTML links.

  • usages that are broken, but the original was broken too because anch didn't properly slugify links (e.g. not replacing things like "/" or "()"). It would be good to fix these while we are here.

  • usages that are broken, but the original was broken too because of some weird line formatting thing.

  • usages like `{{anch(...)}}`, where we we end up with a Markdown link inside a Markdown code element, which doesn't work. Lots of these in <input> elements, and a few in other places.

Ideally I would love it if these updates could only change the files that need to be updated, rather than update the script and redo them all. That way I don't have to re-review all the changes again.

files/en-us/learn/accessibility/wai-aria_basics/index.md Outdated Show resolved Hide resolved
@@ -70,5 +70,5 @@ If you would like your work assessed, or are stuck and want to ask for help:

- A descriptive title such as "Assessment wanted for Float skill test".
- Details of what you have already tried, and what you would like us to do, e.g. if you are stuck and need help, or want an assessment.
- A link to the example you want assessed or need help with, in an online shareable editor (as mentioned in {{anch("Learn Sidebar", "step 1")}} above). This is a good practice to get into — it's very hard to help someone with a coding problem if you can't see their code.
- A link to the example you want assessed or need help with, in an online shareable editor (as mentioned in [step 1](#learn_sidebar) above). This is a good practice to get into — it's very hard to help someone with a coding problem if you can't see their code.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't work but didn't before either.

files/en-us/tools/devtoolsapi/index.html Outdated Show resolved Hide resolved
files/en-us/tools/memory/aggregate_view/index.html Outdated Show resolved Hide resolved
files/en-us/web/html/element/input/range/index.md Outdated Show resolved Hide resolved
files/en-us/web/html/element/input/range/index.md Outdated Show resolved Hide resolved
files/en-us/web/html/element/input/range/index.md Outdated Show resolved Hide resolved
files/en-us/web/html/element/input/reset/index.md Outdated Show resolved Hide resolved
files/en-us/web/html/element/input/search/index.md Outdated Show resolved Hide resolved
@queengooborg
Copy link
Collaborator Author

Thank you both for reviewing! I've updated the script and re-ran it, pushing updates in a way that won't clear the review status on all the unaffected files. I'm avoiding any manual fixes for the time being, such as already-bad anchors, to keep the changeset as small as possible.

Copy link
Collaborator

@wbamberg wbamberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks the the updates! Mostly good now.

@sideshowbarker sideshowbarker removed their request for review March 13, 2022 05:36
@rebloor
Copy link
Contributor

rebloor commented Mar 13, 2022

Again, looks okay for the web extension content.

Copy link
Collaborator

@wbamberg wbamberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one more left that I can see.

files/en-us/learn/accessibility/wai-aria_basics/index.md Outdated Show resolved Hide resolved
Co-authored-by: wbamberg <will@bootbonnet.ca>
Copy link
Collaborator

@wbamberg wbamberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, great work @queengooborg !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:Accessibility Accessibility docs Content:CSS Cascading Style Sheets docs Content:DevTools Firefox DevTools docs (retired) Content:HTML Hypertext Markup Language docs Content:HTTP HTTP docs Content:JS JavaScript docs Content:Learn Learning area docs Content:Media Media docs Content:Other Any docs not covered by another "Content:" label Content:SVG SVG docs Content:wasm WebAssembly docs Content:WebAPI Web API docs Content:WebExt WebExtensions docs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants