Skip to content

Migrate {image} to ![]() #1978

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

Merged
merged 5 commits into from
Mar 24, 2024
Merged

Conversation

anbraten
Copy link
Contributor

@anbraten anbraten commented Feb 24, 2024

closes #1501

The script I've used to migrate
import fs from "fs/promises";
import path from "path";

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()) yield entry;
  }
}

function convertMarkdownToImageMarkdown(markdown: string): string {
  const detectedImageMarkdown = markdown.match(/\`\`\`\{image\}.*?\`\`\`/gis);

  for (const imageMarkdown of detectedImageMarkdown || []) {
    const imageMarkdownParts = imageMarkdown.split("\n");
    const altText = imageMarkdownParts
      .find((part) => part.startsWith(":alt:"))
      ?.replace(":alt:", "")
      .trim();
    const width = imageMarkdownParts
      .find((part) => part.startsWith(":width:"))
      ?.replace(":width:", "")
      .trim();
    const image = imageMarkdownParts
      .find((part) => part.startsWith("```{image}"))
      ?.replace("```{image}", "")
      .trim();

    if (altText && image) {
      const newImageMarkdown = `![${altText}](${image}${
        width ? ` =${width}` : ""
      })`;
      markdown = markdown.replace(imageMarkdown, newImageMarkdown);
    }
  }

  return markdown;
}

async function main() {
  for await (const p of walk(path.join(__dirname, "docs"))) {
    if (p.endsWith(".md")) {
      const markdown = await fs.readFile(p, "utf-8");
      const newMarkdown = convertMarkdownToImageMarkdown(markdown);
      await fs.writeFile(p, newMarkdown, "utf-8");
    }
  }
}

main();

@anbraten anbraten mentioned this pull request Feb 24, 2024
2 tasks
@anbraten anbraten marked this pull request as draft February 27, 2024 11:23
@peterleimbach
Copy link
Collaborator

@psonnera Anton wants to correct the Sphinx syntax to markdown syntax. What do you think shall we merge now or wait for our test with image sizes which need the Sphinx syntax?

@psonnera
Copy link
Collaborator

psonnera commented Mar 2, 2024

@peterleimbach as Markdown doesn't allow image sizing, we might get some using full frame width if they were captured with more than the recommended format. Still, this shouldn't be an issue for phone browsers and even if computer users will have some big ones we probably can review them later. @anbraten script migration sounds like a good idea. We will resume and implement the final image size later.

@anbraten
Copy link
Contributor Author

anbraten commented Mar 2, 2024

Seems like its possible to set custom image sizes with myst using markdown as well: ![fishy](img/fun-fish.png){.bg-warning w=100px align=center} https://myst-parser.readthedocs.io/en/latest/syntax/images_and_figures.html

Using standard markdown sytanx wherever possible will porbably bring the most flexibility in editors / frameworks in the long term.

@psonnera
Copy link
Collaborator

psonnera commented Mar 2, 2024

@anbraten yes, we just need @peterleimbach to confirm it is portable into Crowdin

@anbraten
Copy link
Contributor Author

anbraten commented Mar 2, 2024

Will also have a look at crowdin again and test it as well. I've created a test crowdin project with the translation to tryout some things recently, as I also wanted to help improving the general experience for translators.

@psonnera
Copy link
Collaborator

psonnera commented Mar 2, 2024

@anbraten Sphinx doesn't seem to accept the syntax. I now remember I moved all to html syntax in Nightscout after migrating from MkDocs because of this bug.

@psonnera
Copy link
Collaborator

psonnera commented Mar 3, 2024

@anbraten my bad, needed to add a piece https://myst-parser.readthedocs.io/en/latest/syntax/optional.html#syntax-attributes-inline
@peterleimbach FYI

@anbraten
Copy link
Contributor Author

@psonnera I've added the extension and migrated all images again. While checking I however actually noticed that scaled images are currently only used in docs/EN/Sandbox/imagescaling.md and docs/EN/changelanguage.md anyways.

@anbraten anbraten marked this pull request as ready for review March 19, 2024 17:28
@peterleimbach
Copy link
Collaborator

@anbraten Thank you.

@peterleimbach peterleimbach merged commit 60801aa into openaps:master Mar 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

replace use of {images} with ![]() markdown statement
3 participants