Skip to main content

Adding custom attributes using hashtags.

Project description

This plugin attempt to create inline markdown attribute using hashtags (#) to mimic attribute list but in better.

This plugin will convert all "#contents to **contents**{: #contents .hash} to add custom CSS. Moreover, using a custom css file, you can also style text. The plugin will convert somes text#attribute to somes text**{: #attribute}** using this file as base!

↪️ #2022/01/01 will become **2022/01/01**{: #2022/01 .hash}

Installation

pip install mkdocs-custom-tags-attributes --upgrade

First, add the plugin in your mkdocs.yml:

plugins:
  - search
  - custom-attributes

Note: If you have no plugin entry in your config file yet, you'll likely also want to add the search plugin. MkDocs enables it by default if there is no plugin entry set, but now you have to enable it explicitly.

You need to create an custom_attributes.css if you want to create inline attributes!

Configuration

You can specify the css file in your mkdocs.yml:

plugins:
  - search
  - custom-attributes:
      file: assets/css/custom_attributes.css

Obviously, you need to update your extra css :

extra_css:
  - assets/css/custom_attributes.css

Allowed characters

The only symbols allowed in tags are :

  • _ (underscore)
  • - (dash)
  • / (forward slash)

You can use these case styles/formats :

  • camelCase : #myTag
  • PascalCase : #MyTag
  • snake_case : #my_tag
  • kebab-case : #my-tag

Nested tags (like #myTag/mySubTag) are recognized.

Styling usage

Inline attributes

After this, in the css file, you can add inline attribute, automatically parsed by the plugin. Each tags must be an css id, aka prepend with #.

#yourtags {
/* your css */
}

💭 Don't forget to escape the characters (as \ or / for example!)

Little example : align to right a text.

#right {
    display: inline-block;
    width: 100%;
    text-align: right;
    font-weight: normal;
}

The text :

text to right#right

Will become :

**text to right**{: #right}\n

or in html :

<p><strong id="right">text to right</strong></p>

💭 You can note that I choose to use bold to mark the inline attribute. You can remove it with font-weight: normal; in the css file when specify your tags.

Also, some inlines attribute can be a bit strange. First, any inline attributes placed in the end of the line will be applied on the entire paragraph.

There is a lot of possible example, so you can check the tests to saw behavior. Please, also refer to the attribute list documentation.

original converted attribute html
text to right#right **text to right**{: #right} <p><strong id="right">text to right</strong></p>
#FFXIV **FFXIV**{: #FFXIV .hash} <p><strong class="hash" id="FFXIV">FFXIV</strong></p>
#FFXIV #other **FFXIV**{: #FFXIV .hash} **other**{: #other .hash} <p><strong class="hash" id="FFXIV">FFXIV</strong> <strong class="hash" id="other">other</strong></p>
text1#right text2#right **text1 text2**{: #right} <p><strong id="right">text1 text2</strong></p>
Lorem ipsum dolor#blue sit amet, consectetur adipiscing elit#right **Lorem ipsum **dolor**{: #blue} sit amet, consectetur adipiscing elit**{: #right} <p><strong>Lorem ipsum </strong>dolor<strong id="right">{: #blue} sit amet, consectetur adipiscing elit</strong></p>
to right#right #FFXIV to **right**{: #right} **FFXIV**{: #FFXIV .hash} <p>to <strong id="right">right</strong> <strong class="hash" id="FFXIV">FFXIV</strong></p>
lorem ipsum with #FFXIV and #right[^1] lorem ipsum with **FFXIV**{: #FFXIV .hash} and \n{: #right}\n <p id="right">lorem ipsum with <strong class="hash" id="FFXIV">FFXIV</strong> and </p>

[^1]: Note the absence of word before the last tags.

⚠️️️️ATTENTION⚠️

You need at last one word before each attributes to stylize unless the attributes is in the end of a paragraph. An attribute in the end of a paragraph will stylize all the paragraph.

💭 Error example:

lorem ipsum with #FFXIV and #blue But not right#right -> lorem ipsum with **FFXIV**{: #FFXIV .hash} and #blue But not right\n{: #right}\n

Tags

You can also custom your inline tags (hello obsidian user!) using the .hash class! For example:

.hash {
    background-color: honeydew;
    border-radius: 5px;
}

Test & dev :

  • The conda environment "Publish" list all requirements for developing the plugins.
  • The package is developed using semantic-release, so please respect that.
  • You can use flake8 and pyformat to correct your code.

To test the plugin :

from custom_attributes.plugin import convert_text_attributes, convert_hashtags
import markdown as mk
config = {
        'docs_dir' : 'any_folder'
        'file' : 'path/to/custom_attributes.css'
    }
text = 'any string with custom attributes'
print(convert_text_attributes(text, config))

Functions :

  • read_custom(config: dict[str, str] -> list : Read the css file and take each css ID and return it as a list. Return empty list if file not found.
  • cleanned_word(line: str, word_regex: str) -> str : Check and convert the word before tags attributes if any. Return empty string if no word are found.
  • convert_hashtags (config: dict[str, str], line: str) -> str: Convert the tags attributes from the list when reading a line.
  • convert_text_attributes(markdown: str, config: dict[str, str]) -> str : Read an entire Markdown text to convert line per line the hashtags and tags attributes.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mkdocs-custom-tags-attributes-0.3.3.tar.gz (20.8 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file mkdocs-custom-tags-attributes-0.3.3.tar.gz.

File metadata

File hashes

Hashes for mkdocs-custom-tags-attributes-0.3.3.tar.gz
Algorithm Hash digest
SHA256 460812fa938272c92852fec3d824f798e44d580ef4a973321cd05a875147362f
MD5 4f5793800c6c694a6536c46c2b9d44dd
BLAKE2b-256 764110c3f50367e6043200682de7b1feda4a077417437a2c3a44f9996fefd015

See more details on using hashes here.

File details

Details for the file mkdocs_custom_tags_attributes-0.3.3-py3-none-any.whl.

File metadata

File hashes

Hashes for mkdocs_custom_tags_attributes-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 b7fdc87ae45f7ba59f3294bad9f51cc98e715922bd63ae68dab407b39a0eb619
MD5 3a0641cebe0c6e58d6ed38369a1aa2db
BLAKE2b-256 f029e01b873322fe5a2820540e0197de4f8b18e9c85d4cce92e7f6e4180aa9c2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page