This example demonstrates an HTTP Trigger Function providing markdown parsing with marked. Default render and lex actions are supported, along with highlight.js code highlighting.
Example uses the Express framework.
See file functions/index.js for the code.
The dependencies are listed in functions/package.json.
- Clone or download this repo and open the
markdown
directory. - If you don't already have one, create a Firebase Project using the Firebase Console.
- If the Firebase CLI is not install, install it with
npm install -g firebase-tools
and then configure it withfirebase login
. - Configure the CLI locally by using
firebase use --add
and select your project in the list. - Install dependencies locally by running:
cd functions; npm install; cd -
IMPORTANT:
Cloud Function only supports POST requests.
Requests to Cloud Function should specify header contentType
of application/json
and contain the following fields:
- content - markdown content
- action - action to be performed by Cloud Function (possible values: "RENDER" or "LEX")
- (optional) options - marked parser options - see marked options for more information
- All marked options are supported EXCEPT
renderer
- To highlight code, send
highlight: true
option instead of a highlight function - Cloud Function uses highlight.js for all code highlighting
- All marked options are supported EXCEPT
Example Request:
{
"content": "# Hello World",
"action": "RENDER",
"options": {
"highlight": true,
"gfm": false
}
}
- Deploy your project using
firebase deploy --only functions
.
Example Google Apps Script to call deployed Cloud Function for markdown render action.
...
function getRenderedMarkdown() {
var response, responseCode;
// DEVELOPER TODO: Replace "functionUrl" with the deployed Cloud Function URL from the Firebase Console
// Consider storing URL in a Google Apps Script Properties Store
var functionUrl = "https://<FIREBASE_CLOUD_FUNCTION_URL>/markdown";
// DEVELOPER TODO: Replace markdown content
var markdown = "# Example Code\n\n```js\n console.log('hello'); \n```";
var requestBody = {
content: markdown,
action: "RENDER",
options: {
highlight: true
}
}
var params = {
"method": "POST",
"contentType": "application/json",
"payload": JSON.stringify(requestBody)
};
response = UrlFetchApp.fetch(functionUrl + "/", params);
responseCode = response.getResponseCode();
if (responseCode == 200) {
// Success fetching file from Cloud Function
return response.getContentText();
} else {
// Error returned from Cloud Function
return null;
}
}
...
Example Google Apps Script to call deployed Cloud Function for markdown lex action.
...
function getLexedTokens() {
var response, responseCode;
// DEVELOPER TODO: Replace "functionUrl" with the deployed Cloud Function URL from the Firebase Console
// Consider storing URL in a Google Apps Script Properties Store
var functionUrl = "https://<FIREBASE_CLOUD_FUNCTION_URL>/markdown";
// DEVELOPER TODO: Replace markdown content
var markdown = "# Example Code\n\n```js\n console.log('hello'); \n```";
var payload = {
content: markdown,
action: "LEX"
}
var params = {
"method": "POST",
"contentType": "application/json",
"payload": JSON.stringify(payload)
};
response = UrlFetchApp.fetch(functionUrl + "/", params);
responseCode = response.getResponseCode();
if (responseCode == 200) {
// Success fetching file from Cloud Function
return response.getContentText();
} else {
// Error returned from Cloud Function
return null;
}
}
...
© Laura Taylor. Licensed under an MIT license.