How can I display text with code snippets in database [closed]

Discussão em 'Angular' iniciado por Murad, Março 9, 2025.

  1. Murad

    Murad Guest

    I'm building a basic IT community website using Angular for the frontend and a backend API (C#) with a database (PostgreSQL). There is an article section that includes both text explanations and code snippets.

    Requirements

    • The database should store both normal text and code snippets
    • The frontend should display the code with syntax highlighting (e.g., using ngx-highlightjs)
    • The API should return the data in a structured format that allows easy rendering
    • The solution should be secure (avoid XSS, SQL injection, etc.)

    Approach I'm considering

    Separate columns for text and code types in the database, then return a response something like this:

    {
    "title": "How does async/await work?",
    "sections": [
    {
    "type": "text",
    "content": "Async/await allows writing async code in a sync manner."
    },
    {
    "type": "code",
    "language": "javascript",
    "content": "async function fetchData() { await fetch('url'); }"
    },
    {
    "type": "text",
    "content": "The 'await' keyword pauses execution until the promise resolves."
    }
    ]
    }


    Questions

    • Is this the best way to structure a database for storing both text and code snippets?
    • How can I prevent XSS attacks when displaying the code in Angular?
    • Are there better approaches for storing and retrieving code snippets efficiently (SQL vs. NoSQL)?

    Continue reading...

Compartilhe esta Página