# Dockerfile for markdown to PDF conversion with Mermaid support
FROM node:18-alpine

# Install chromium and required dependencies for puppeteer
RUN apk add --no-cache \
    chromium \
    nss \
    freetype \
    freetype-dev \
    harfbuzz \
    ca-certificates \
    ttf-freefont

# Install mermaid CLI globally
RUN npm install -g @mermaid-js/mermaid-cli

# Set Puppeteer to use the system chromium installation
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true \
    PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser

# Create app directory
WORKDIR /app

# Copy the conversion script first (as root)
COPY convert-docs.js /app/convert-docs.js

# Create package.json and install local dependencies
RUN echo '{"name": "docs-converter", "version": "1.0.0", "dependencies": {"marked": "^11.0.0"}}' > package.json && \
    npm install

# Create non-root user for security
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nodejs -u 1001

# Change ownership of the app directory
RUN chown -R nodejs:nodejs /app
USER nodejs

# Entry point will be a custom script
ENTRYPOINT ["node", "/app/convert-docs.js"]