function
This commit is contained in:
7
.funcignore
Normal file
7
.funcignore
Normal file
@@ -0,0 +1,7 @@
|
||||
*.js.map
|
||||
*.ts
|
||||
.git*
|
||||
.vscode
|
||||
local.settings.json
|
||||
test
|
||||
getting_started.md
|
||||
42
.gitignore
vendored
42
.gitignore
vendored
@@ -1 +1,43 @@
|
||||
bin
|
||||
obj
|
||||
csx
|
||||
.vs
|
||||
edge
|
||||
Publish
|
||||
|
||||
*.user
|
||||
*.suo
|
||||
*.cscfg
|
||||
*.Cache
|
||||
project.lock.json
|
||||
|
||||
/packages
|
||||
/TestResults
|
||||
|
||||
/tools/NuGet.exe
|
||||
/App_Data
|
||||
/secrets
|
||||
/data
|
||||
.secrets
|
||||
appsettings.json
|
||||
local.settings.json
|
||||
|
||||
node_modules
|
||||
dist
|
||||
|
||||
# Local python packages
|
||||
.python_packages/
|
||||
|
||||
# Python Environments
|
||||
.env
|
||||
.venv
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
5
.vscode/extensions.json
vendored
Normal file
5
.vscode/extensions.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"recommendations": [
|
||||
"ms-azuretools.vscode-azurefunctions"
|
||||
]
|
||||
}
|
||||
12
.vscode/launch.json
vendored
Normal file
12
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Attach to Node Functions",
|
||||
"type": "node",
|
||||
"request": "attach",
|
||||
"port": 9229,
|
||||
"preLaunchTask": "func: host start"
|
||||
}
|
||||
]
|
||||
}
|
||||
8
.vscode/settings.json
vendored
Normal file
8
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"azureFunctions.deploySubpath": ".",
|
||||
"azureFunctions.postDeployTask": "npm install (functions)",
|
||||
"azureFunctions.projectLanguage": "TypeScript",
|
||||
"azureFunctions.projectRuntime": "~3",
|
||||
"debug.internalConsoleOptions": "neverOpen",
|
||||
"azureFunctions.preDeployTask": "npm prune (functions)"
|
||||
}
|
||||
31
.vscode/tasks.json
vendored
Normal file
31
.vscode/tasks.json
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
{
|
||||
"version": "2.0.0",
|
||||
"tasks": [
|
||||
{
|
||||
"type": "func",
|
||||
"command": "host start",
|
||||
"problemMatcher": "$func-node-watch",
|
||||
"isBackground": true,
|
||||
"dependsOn": "npm build (functions)"
|
||||
},
|
||||
{
|
||||
"type": "shell",
|
||||
"label": "npm build (functions)",
|
||||
"command": "npm run build",
|
||||
"dependsOn": "npm install (functions)",
|
||||
"problemMatcher": "$tsc"
|
||||
},
|
||||
{
|
||||
"type": "shell",
|
||||
"label": "npm install (functions)",
|
||||
"command": "npm install"
|
||||
},
|
||||
{
|
||||
"type": "shell",
|
||||
"label": "npm prune (functions)",
|
||||
"command": "npm prune --production",
|
||||
"dependsOn": "npm build (functions)",
|
||||
"problemMatcher": []
|
||||
}
|
||||
]
|
||||
}
|
||||
20
HttpTrigger/function.json
Normal file
20
HttpTrigger/function.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"bindings": [
|
||||
{
|
||||
"authLevel": "function",
|
||||
"type": "httpTrigger",
|
||||
"direction": "in",
|
||||
"name": "req",
|
||||
"methods": [
|
||||
"get",
|
||||
"post"
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "http",
|
||||
"direction": "out",
|
||||
"name": "res"
|
||||
}
|
||||
],
|
||||
"scriptFile": "../dist/HttpTrigger/index.js"
|
||||
}
|
||||
43
HttpTrigger/index.ts
Normal file
43
HttpTrigger/index.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { AzureFunction, Context, HttpRequest } from "@azure/functions"
|
||||
import * as Prism from 'prismjs';
|
||||
import * as loadLanguages from 'prismjs/components/index.js';
|
||||
loadLanguages();
|
||||
|
||||
const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {
|
||||
if(req.method === "GET") {
|
||||
context.res = {
|
||||
// status: 200, /* Defaults to 200 */
|
||||
body: "Ok"
|
||||
};
|
||||
return;
|
||||
}
|
||||
const data = req.rawBody;
|
||||
if(!data.startsWith('```')) {
|
||||
context.res = {
|
||||
status: 200,
|
||||
body: req.rawBody
|
||||
}
|
||||
return
|
||||
}
|
||||
const regex = data.match(/^```([a-zA-Z]*)$/m)
|
||||
const lang = regex[1]
|
||||
if(!lang) {
|
||||
context.res = {
|
||||
status: 200,
|
||||
body: req.rawBody
|
||||
}
|
||||
return
|
||||
}
|
||||
const highlighted = Prism.highlight(data.replace(/```[a-zA-Z]*/m, '').replace('```', ''), Prism.languages[lang], lang)
|
||||
|
||||
// remove any backticks and the language
|
||||
const highlightedCleaned = highlighted.trim()
|
||||
context.res = {
|
||||
status: 200,
|
||||
body: `<pre><code>${highlightedCleaned}</code></pre>`
|
||||
}
|
||||
return
|
||||
|
||||
};
|
||||
|
||||
export default httpTrigger;
|
||||
@@ -1 +1 @@
|
||||
This is a fastify nodejs server that parses and runs prism on the server. This is used by my blog which does not run Node and cannot use prism. I don't use prism's JS file on the client to avoid any lighthouse impact
|
||||
This is an azure nodejs function that parses and runs prism on the server. This is used by my blog which does not run Node and cannot use prism. I don't use prism's JS file on the client to avoid any lighthouse impact
|
||||
15
host.json
Normal file
15
host.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"version": "2.0",
|
||||
"logging": {
|
||||
"applicationInsights": {
|
||||
"samplingSettings": {
|
||||
"isEnabled": true,
|
||||
"excludedTypes": "Request"
|
||||
}
|
||||
}
|
||||
},
|
||||
"extensionBundle": {
|
||||
"id": "Microsoft.Azure.Functions.ExtensionBundle",
|
||||
"version": "[2.*, 3.0.0)"
|
||||
}
|
||||
}
|
||||
38
index.mjs
38
index.mjs
@@ -1,38 +0,0 @@
|
||||
import Fastify from 'fastify';
|
||||
import Prism from 'prismjs';
|
||||
import loadLanguages from 'prismjs/components/index.js';
|
||||
|
||||
loadLanguages();
|
||||
|
||||
const fastify = Fastify({
|
||||
logger: true
|
||||
})
|
||||
fastify.get('/', async (request, reply) => {
|
||||
return reply.status(200).send(`ok`)
|
||||
})
|
||||
// Declare a route
|
||||
fastify.post('/', function (request, reply) {
|
||||
const data = request.body?.trim()
|
||||
if(!data.startsWith('```')) {
|
||||
return reply.send(request.body)
|
||||
}
|
||||
const regex = data.match(/^```([a-zA-Z]*)$/m)
|
||||
const lang = regex[1]
|
||||
if(!lang) {
|
||||
return reply.send(request.body)
|
||||
}
|
||||
const highlighted = Prism.highlight(data.replace(/```[a-zA-Z]*/m, '').replace('```', ''), Prism.languages[lang], lang)
|
||||
|
||||
// remove any backticks and the language
|
||||
const highlightedCleaned = highlighted.trim()
|
||||
return `<pre><code>${highlightedCleaned}</code></pre>`
|
||||
})
|
||||
|
||||
// Run the server!
|
||||
fastify.listen(process.env.PORT || 3000, '0.0.0.0', function (err, address) {
|
||||
if (err) {
|
||||
fastify.log.error(err)
|
||||
process.exit(1)
|
||||
}
|
||||
console.log(`Server is now listening on ${address}`)
|
||||
})
|
||||
3308
package-lock.json
generated
3308
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
26
package.json
26
package.json
@@ -1,17 +1,25 @@
|
||||
{
|
||||
"name": "prismasaservice",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.mjs",
|
||||
"version": "",
|
||||
"scripts": {
|
||||
"start": "node ./index.mjs"
|
||||
"build": "tsc",
|
||||
"build:production": "npm run prestart && npm prune --production",
|
||||
"watch": "tsc --w",
|
||||
"prestart": "npm run build && func extensions install",
|
||||
"start:host": "func start",
|
||||
"start": "npm-run-all --parallel start:host watch",
|
||||
"test": "echo \"No tests yet...\""
|
||||
},
|
||||
"description": "",
|
||||
"devDependencies": {
|
||||
"@azure/functions": "^1.0.1-beta1",
|
||||
"npm-run-all": "^4.1.5",
|
||||
"typescript": "^3.3.3"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"fastify": "^3.27.4",
|
||||
"fastify-cli": "^2.15.0",
|
||||
"prismjs": "^1.27.0"
|
||||
},
|
||||
"volta": {
|
||||
"node": "14.19.0"
|
||||
}
|
||||
}
|
||||
|
||||
10
tsconfig.json
Normal file
10
tsconfig.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"target": "es6",
|
||||
"outDir": "dist",
|
||||
"rootDir": ".",
|
||||
"sourceMap": true,
|
||||
"strict": false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user