<commit_before>'use strict';
const WPAPI = require( 'wpapi' );

function renderFullPage(renderedContent, data) {
    data = safeStringify(data)
  return `<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<link href='https://fonts.googleapis.com/css?family=Roboto:400,300,500' rel='stylesheet' type='text/css'>
	</head>
	<body>
		<div id="container">${renderedContent}</div>
        <script>
            var props = ${data};
            console.log(props);
        </script>
	</body>
</html>`;
}
function safeStringify(obj) {
  return JSON.stringify(obj).replace(/<\/script/g, '<\\/script').replace(/<!--/g, '<\\!--');
}

function createListHtml(data) {
    let html = "<main>";
    for (var i = 0; i < data.length; i++) {
        html += "<article>"
        html += "<h1>" + data[i].title.rendered  + "</h1>";
        html += data[i].excerpt.rendered;
        html += "</article>";
    }
    html += '</main>';
    return html
}

module.exports.index = (event, context, callback) => {
    const wp = new WPAPI({ endpoint: 'http://wp-kyoto.net/wp-json' });
    wp.posts().then(function( data ) {
      const renderedContent = createListHtml(data)
      const renderedPage = renderFullPage( renderedContent, data );
      callback(null, renderedPage);
    }).catch(function( err ) {
        callback(err);
    });
};<commit_msg>create html component by React<commit_after>'use strict';
const WPAPI = require( 'wpapi' );
import React, { Component } from 'react';
import { renderToString } from 'react-dom/server';

class App extends Component {
    constructor(props, context){
        super(props)
    }
    render() {
        return (
            <div id="container">
                <LambdaInfo context={this.props.context} />
                <WPPosts posts={this.props.posts} />
            </div>
        )
    }
}

class LambdaInfo extends Component {
    constructor(props, context){
        super(props)
    }
    render() {
        const context = this.props.context
        return (
            <header>
                <h1>Lambda Info</h1>
                <table>
                    <tr><th>invokedFunctionArn</th><td>{context.invokedFunctionArn}</td></tr>
                    <tr><th>logGroupName</th><td>{context.logGroupName}</td></tr>
                </table>
            </header>
        )
    }
}

class WPPost extends  Component {
    constructor(props, context) {
        super(props)
    }
    render() {
        const post = this.props.post
        return (
            <archive key={post.id}>
                <h2>{post.title.rendered}</h2>
                <div dangerouslySetInnerHTML={{__html: post.excerpt.rendered}} />
            </archive>
        )
    }
}

class WPPosts extends Component {
    constructor(props, context) {
        super(props)
    }
    render() {
        const postList = this.props.posts.map( post => {
            return <WPPost post={post} />
        })
        return (
            <main>
                <h1>WP Postlist</h1>
                {postList}
            </main>
        )
    }
}

function renderFullPage(renderedContent, context) {
    context = safeStringify(context)
  return `<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<link href='https://fonts.googleapis.com/css?family=Roboto:400,300,500' rel='stylesheet' type='text/css'>
	</head>
	<body>
		${renderedContent}
        <script>
            var props = ${context};
            console.log(props);
        </script>
	</body>
</html>`;
}

function safeStringify(obj) {
  return JSON.stringify(obj).replace(/<\/script/g, '<\\/script').replace(/<!--/g, '<\\!--');
}

module.exports.index = (event, context, callback) => {
    const wp = new WPAPI({ endpoint: 'https://api.wp-app.org/wp-json' });
    wp.posts().then(function( data ) {
      const renderedContent = renderToString( <App posts={data} context={context}/>);
      const renderedPage = renderFullPage( renderedContent, context );
      callback(null, renderedPage);
    }).catch(function( err ) {
        callback(err);
    });
};
{'fraction_non_alphanumeric': 0.16778356912385206, 'fraction_numerical': 0.0071978158351948375, 'mean_word_length': 2.284433577832111, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 69, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3250215', 'n_tokens_mistral': 1273, 'n_tokens_neox': 1221, 'n_words': 245}