Enhance Your App with Markdown Rendering Predictions Using zeke/cog-markdown-example Actions

23 Apr 2025
Enhance Your App with Markdown Rendering Predictions Using zeke/cog-markdown-example Actions

In today’s development landscape, integrating rich content formats like Markdown can significantly enhance user experience. The zeke/cog-markdown-example API provides a powerful Cognitive Action that predicts how Markdown text is rendered, especially when it involves fenced code blocks, giving developers a visual interpretation of the formatted content. In this post, we’ll explore this action, its capabilities, and how you can seamlessly integrate it into your applications.

Prerequisites

To get started with the zeke/cog-markdown-example Cognitive Action, you'll need to ensure you have the following:

  • An API key for the Cognitive Actions platform.
  • Basic knowledge of making API calls using JSON format.

Authentication is typically managed by passing your API key in the request headers.

Cognitive Actions Overview

Predict Markdown Text Rendering

The Predict Markdown Text Rendering action allows you to visualize how Markdown text, particularly with fenced code blocks, will be rendered. This action is particularly useful for applications that need to display formatted code snippets alongside other content.

Input

The input required for this action is structured as follows:

  • markdownText: A string containing the text in Markdown format. This property supports fenced code blocks and is intended for displaying formatted text and code examples.

Example Input JSON:

{
  "markdownText": "```py\ndef allow_iframe_youtube(tag, name, value):\n    if name == \"src\" and value.startswith(\"https://www.youtube.com/embed/\"):\n        return True\n    if name in (\"allow\", \"width\", \"height\"):\n        return True\n    return False\n\n\ndef allow_fenced_code_languages(tag, name, value):\n    return (\n        tag == \"code\"\n        and name == \"class\"\n        and value.startswith(\"language-\")\n        and \" \" not in value\n    )\n```"
}

Output

The output of the action is a URL that points to the rendered Markdown content. This allows you to display the formatted output in your application seamlessly.

Example Output:

https://assets.cognitiveactions.com/invocations/13bccc17-5d27-485f-92f7-09b30941d136/17b5090e-b8de-4886-8d4f-180c6e4a8295.md

Conceptual Usage Example (Python)

Here’s how you might call this action using Python, making sure to construct the input JSON payload properly:

import requests
import json

# Replace with your Cognitive Actions API key and endpoint
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
COGNITIVE_ACTIONS_EXECUTE_URL = "https://api.cognitiveactions.com/actions/execute"  # Hypothetical endpoint

action_id = "5079dff1-c102-4c00-99df-04d7debb2c97"  # Action ID for Predict Markdown Text Rendering

# Construct the input payload based on the action's requirements
payload = {
    "markdownText": "```py\ndef allow_iframe_youtube(tag, name, value):\n    if name == \"src\" and value.startswith(\"https://www.youtube.com/embed/\"):\n        return True\n    if name in (\"allow\", \"width\", \"height\"):\n        return True\n    return False\n\n\ndef allow_fenced_code_languages(tag, name, value):\n    return (\n        tag == \"code\"\n        and name == \"class\"\n        and value.startswith(\"language-\")\n        and \" \" not in value\n    )\n```"
}

headers = {
    "Authorization": f"Bearer {COGNITIVE_ACTIONS_API_KEY}",
    "Content-Type": "application/json"
}

try:
    response = requests.post(
        COGNITIVE_ACTIONS_EXECUTE_URL,
        headers=headers,
        json={"action_id": action_id, "inputs": payload}  # Hypothetical structure
    )
    response.raise_for_status()  # Raise an exception for bad status codes (4xx or 5xx)

    result = response.json()
    print("Action executed successfully:")
    print(json.dumps(result, indent=2))

except requests.exceptions.RequestException as e:
    print(f"Error executing action {action_id}: {e}")
    if e.response is not None:
        print(f"Response status: {e.response.status_code}")
        try:
            print(f"Response body: {e.response.json()}")
        except json.JSONDecodeError:
            print(f"Response body: {e.response.text}")

In this example, you need to replace YOUR_COGNITIVE_ACTIONS_API_KEY with your actual API key. The payload variable contains the structured input as specified. The output will provide you with a URL to the rendered Markdown.

Conclusion

The Predict Markdown Text Rendering action from the zeke/cog-markdown-example API allows developers to enhance their applications with rich Markdown rendering capabilities. By integrating this action, you can provide users with a visually appealing way to display code snippets and formatted text. Consider exploring additional use cases where Markdown rendering can improve content presentation in your applications. Happy coding!