elastic / docs-builder

https://elastic.github.io/docs-builder/
Apache License 2.0
0 stars 0 forks source link

code directive #7

Open Mpdreamz opened 2 weeks ago

Mpdreamz commented 2 weeks ago

Code

Kind Link
Reference https://mystmd.org/guide/directives#directive-code
Documentation https://mystmd.org/guide/code

A code-block environment with a language as the argument, and options for highlighting, showing line numbers, and an optional filename.

Implementation

Specification compliance:

KOTungseth commented 1 week ago

Context:

I'm also including my own notes ⬇️

Purpose

Present code clearly: Code blocks provide a visually distinct area for displaying code snippets, making it easier for users to read and understand programming syntax. This separation from regular text helps highlight important code sections.

Enhance readability: By formatting code with consistent indentation, spacing, and syntax highlighting, code blocks improve readability. This helps users quickly grasp the structure and flow of the code, which is especially important for complex code examples.

Facilitate copying: Code blocks are often designed to allow users to easily copy and paste code into their development environments. This convenience reduces errors caused by manual transcription and speeds up the implementation process.

Demonstrate functionality: Code blocks can illustrate specific functionality or usage examples, helping users understand how to implement a feature, use a library, or interact with an API. They provide practical context for the information being discussed.

Support multi-language examples: Documentation often needs to cover multiple programming languages. Code blocks allow for clear distinctions between different language examples, ensuring users know which code applies to their specific programming environment.

Enable syntax highlighting: Many documentation systems support syntax highlighting for code blocks, which enhances the visibility of keywords, comments, and other elements. This visual differentiation helps users quickly identify relevant parts of the code.

Encourage best practices: Code blocks can be used to showcase best practices, coding standards, or common pitfalls, guiding users in writing better, more efficient code. This is valuable for both novice and experienced developers.

Best practices

Use syntax highlighting: Leverage syntax highlighting to enhance the readability of code blocks. This visual distinction helps users quickly identify keywords, variables, and other elements, making it easier to understand the code.

Provide clear and descriptive context: Before or after the code block, include a brief explanation of what the code does, its purpose, and how it fits into the overall documentation. This context helps users understand why the code is relevant.

Keep code blocks short: Aim to keep code blocks concise and focused on a single concept or function. If a longer example is necessary, consider breaking it into smaller sections or providing a link to a more extensive resource.

Ensure code is executable: If possible, provide code that users can easily copy and paste into their development environments without modification. This reduces errors and enhances the user experience.

Include comments in code: Use comments within the code to explain critical parts or complex logic. This can help users understand the code's intent and facilitate learning.

Provide examples and use cases: Whenever applicable, follow code blocks with real-world examples or scenarios that demonstrate how the code can be used. This practical application helps users see the value of the code in context.

Test code for accuracy: Ensure that the code provided in the documentation is accurate, functional, and up-to-date. Regularly test code examples to verify they work as intended.

Indicate language: Clearly indicate the programming language used in each code block, especially when the documentation covers multiple languages. This helps users quickly identify the relevant syntax.