|
| 1 | +from mkdocs.plugins import BasePlugin |
| 2 | +from mkdocs.config import config_options |
| 3 | +import os |
| 4 | + |
| 5 | +class DefaultMetaPlugin(BasePlugin): |
| 6 | + # A mapping for special case language names |
| 7 | + SPECIAL_CASES = { |
| 8 | + 'typescript': 'TypeScript', |
| 9 | + 'javascript': 'JavaScript', |
| 10 | + 'csharp': 'C#', |
| 11 | + 'objective-c': 'Objective-C', |
| 12 | + } |
| 13 | + |
| 14 | + def format_language_name(self, filename): |
| 15 | + """Format language name correctly based on filename.""" |
| 16 | + language = os.path.splitext(filename)[0] |
| 17 | + return self.SPECIAL_CASES.get(language, language.capitalize()) |
| 18 | + |
| 19 | + def on_page_markdown(self, markdown, page, config, files): |
| 20 | + # Basic site info |
| 21 | + site_url = config.get('site_url', 'https://styles.goatbytes.io') |
| 22 | + default_image = f"{site_url}assets/img/social.jpg" |
| 23 | + |
| 24 | + # Extract and format the language name from the file name |
| 25 | + language = self.format_language_name(os.path.basename(page.file.src_path)) |
| 26 | + custom_title = f"{language} Code Style Guide | GoatStyles" |
| 27 | + custom_description = f"The official {language} code style guide used by GoatBytes.IO." |
| 28 | + |
| 29 | + # Default meta tags with dynamic title and description |
| 30 | + defaults = [ |
| 31 | + {'name': 'description', 'content': custom_description}, |
| 32 | + {'property': 'og:type', 'content': 'website'}, |
| 33 | + {'property': 'og:title', 'content': custom_title}, |
| 34 | + {'property': 'og:description', 'content': custom_description}, |
| 35 | + {'property': 'og:image', 'content': default_image}, |
| 36 | + {'property': 'og:url', 'content': site_url}, |
| 37 | + {'name': 'twitter:card', 'content': 'summary_large_image'}, |
| 38 | + {'name': 'twitter:title', 'content': custom_title}, |
| 39 | + {'name': 'twitter:description', 'content': custom_description}, |
| 40 | + {'name': 'twitter:image', 'content': default_image}, |
| 41 | + ] |
| 42 | + |
| 43 | + # Initialize or update page meta |
| 44 | + if 'meta' not in page.meta: |
| 45 | + page.meta['meta'] = defaults |
| 46 | + else: |
| 47 | + # Update existing tags or add defaults if missing |
| 48 | + existing_tags = {tag.get('name') or tag.get('property'): tag for tag in page.meta['meta']} |
| 49 | + for default in defaults: |
| 50 | + key = default.get('name') or default.get('property') |
| 51 | + if key not in existing_tags: |
| 52 | + page.meta['meta'].append(default) |
| 53 | + elif key in ['description', 'og:title', 'og:description', 'twitter:title', 'twitter:description']: |
| 54 | + # Update content for specific tags if they already exist |
| 55 | + existing_tags[key]['content'] = default['content'] |
| 56 | + |
| 57 | + return markdown |
0 commit comments