mirror of https://github.com/MaxLeiter/Drift
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
	
	
		
			40 lines
		
	
	
		
			803 B
		
	
	
	
		
			TypeScript
		
	
			
		
		
	
	
			40 lines
		
	
	
		
			803 B
		
	
	
	
		
			TypeScript
		
	
import markdown from "../render-markdown"
 | 
						|
import type { File } from "app/prisma"
 | 
						|
/**
 | 
						|
 * returns rendered HTML from a  Drift file
 | 
						|
 */
 | 
						|
function getHtmlFromFile({ content, title }: Pick<File, "content" | "title">) {
 | 
						|
	const renderAsMarkdown = [
 | 
						|
		"markdown",
 | 
						|
		"md",
 | 
						|
		"mdown",
 | 
						|
		"mkdn",
 | 
						|
		"mkd",
 | 
						|
		"mdwn",
 | 
						|
		"mdtxt",
 | 
						|
		"mdtext",
 | 
						|
		"text",
 | 
						|
		""
 | 
						|
	]
 | 
						|
	const fileType = () => {
 | 
						|
		const pathParts = title.split(".")
 | 
						|
		const language = pathParts.length > 1 ? pathParts[pathParts.length - 1] : ""
 | 
						|
		return language
 | 
						|
	}
 | 
						|
	const type = fileType()
 | 
						|
	let contentToRender: string = content || ""
 | 
						|
 | 
						|
	if (!renderAsMarkdown.includes(type)) {
 | 
						|
		contentToRender = `~~~${type}
 | 
						|
${content}
 | 
						|
~~~`
 | 
						|
	} else {
 | 
						|
		contentToRender = "\n" + content
 | 
						|
	}
 | 
						|
 | 
						|
	const html = markdown(contentToRender)
 | 
						|
	return html
 | 
						|
}
 | 
						|
 | 
						|
export default getHtmlFromFile
 |