alfredapp / openai-workflow

OpenAI integrations
BSD 3-Clause "New" or "Revised" License
67 stars 33 forks source link

Handle code blocks nested within Markdown #50

Closed jklewa closed 3 weeks ago

jklewa commented 3 weeks ago

Frequently Asked Questions

Workflow version

v2024.17

Alfred version

5.5.1 [2273]

macOS version

Sequoia 15.1 (24B83)

Debugger output

[23:19:34.204] ChatGPT / DALL-E[Text View] Running with argument ''
[23:19:34.260] ChatGPT / DALL-E[Text View] Script with argv '' finished
[23:19:34.265] ChatGPT / DALL-E[Text View] {"response":"> Give me examples of markdown, including nested and multiple code blocks\n\nCertainly! Markdown is a lightweight markup language used to format text. Here are some examples, including nested and multiple code blocks:\n\n### Basic Text Formatting\n```markdown\n# Heading 1\n## Heading 2\n### Heading 3\n\n**Bold Text**\n\n*Italic Text*\n\n[Link to OpenAI](https://www.openai.com)\n\n- Bullet list item 1\n- Bullet list item 2\n\n1. Numbered list item 1\n2. Numbered list item 2\n```\n\n### Code Blocks\n```markdown\nInline code: `print(\"Hello, World!\")`\n\nCode block with syntax highlighting (Python):\n```python\ndef greet(name):\n    return f\"Hello, {name}!\"\n\nprint(greet(\"World\"))\n```\n```\n\n### Nested Code Blocks\nMarkdown doesn't support nested code blocks directly, but you can achieve a similar effect using lists:\n\n```markdown\n1. Step involving code:\n\n    ```python\n    def do_something():\n        pass\n    ```\n\n2. Another step with different code:\n\n    ```bash\n    echo \"Hello from bash\"\n    ```\n```\n\n### Multiple Code Blocks\n```markdown\nHere is a JavaScript example:\n\n```javascript\nfunction sayHi() {\n    console.log(\"Hi!\");\n}\nsayHi();\n```\n\nAnd here is a JSON example:\n\n```json\n{\n    \"name\": \"John\",\n    \"age\": 30,\n    \"isStudent\": false\n}\n```\n```\n\n### Blockquotes\n```markdown\n> This is a blockquote.\n>\n> > Nested blockquote with more information.\n>\n> Concluding remarks.\n```\n\n### Tables\n```markdown\n| Syntax | Description |\n|--------|-------------|\n| Header | Title       |\n| Paragraph | Text     |\n```\n\n### Horizontal Rule\n```markdown\n---\n\nor\n\n***\n```\n\nThese examples cover some of the basic and slightly more complex structures you can create with Markdown.\n\n","behaviour":{"scroll":"end"}}

More details

When another code block is nested within a markdown code block, Alfred has trouble formatting the output.

Alfred's Markdown Syntax doesn't specify how nested code blocks should behave.

This is an excerpt from ChatGPT's response using a prompt similar to "Examples of Markdown":

### Multiple Code Blocks
```markdown
Here is a JavaScript example:

```javascript
function sayHi() {
    console.log("Hi!");
}
sayHi();
```

And here is a JSON example:

```json
{
    "name": "John",
    "age": 30,
    "isStudent": false
}
```
```

Current

chatgpt_—_user_workflow_23D53FFB-6495-4BEB-AD89-432D664C3FE5

Updated

Updated behavior
vitorgalvao commented 3 weeks ago

Thank you for the submission and the heads-up. This is worth thinking about, though I’m not convinced of the approach. One important (but fixable) aspect is the code goes against the functional style of the whole script. Seeing as I’d have to maintain this code after merging and that this is in essence a hack (I don’t say that pejoratively), it’s particularly important that it follows the code style and be consistent with the rest of the logic.

While reflecting and experimenting with this, I bumped into an interesting observation. ChatGPT itself doesn’t do this correctly:

Nor does GitHub, for that matter. This (which I had to render render with HTML for demonstration purposes):

```markdown
# This is an h1 header

Some Python code

```python
def inner_function():
    print("Inner function")

inner_function()
```

Some more text

## An h2 header

More text here

```

Becomes:

Which on first glance makes me think this will probably be more complicated than it looks at first glance. Not because of ChatGPT, but GitHub puts a lot of effort into Markdown. And while I appreciate the effort on the fix, I also don’t think it’s the right approach to work around this only on this workflow, because the Text View is generic. It should either be done in Alfred as a whole, or not at all.

I’ll open an issue internally to discuss this, to determine how viable it is. But since you marked this as draft I wanted to let you know sooner rather than later so you wouldn’t spend too much time on it. Although, as always, you’re welcome to use your own changes in your own local installation, as long as those are disclosed when opening bug reports.

Thank you again for the submission, and especially for bringing this to my attention. Have a relaxed Sunday!