Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>NYT Top Stories</h1>

<p>Here are today's top stories from <a href="https://www.nytimes.com">The New York Times</a>.</p>

<div id="app">
  <p>
    <b>Fetching today's top stories...</b>
  </p>
</div>
              
            
!

CSS

              
                * {
  box-sizing: border-box;
}

body {
  width: 88%;
  max-width: 40rem;
  margin: 1rem auto;
  font: 1.25rem/1.5 Arial, Helvetica, sans-serif;
  color: #222;
}

h1,
h2,
h3 {
  line-height: 1.25;
  font-family: Georgia, 'Times New Roman', Times, serif;
}

:focus {
  outline: .125rem dotted currentColor;
  outline-offset: .25rem;
}
              
            
!

JS

              
                //
// Variables
//

// Get the #app element
const app = document.querySelector('#app');

// Store the API endpoint
const endpoint = 'https://nyt.barker.workers.dev';


//
// Functions
//

/**
 * Get the JSON data from a Fetch request
 * @param {Response} response The Response object
 * @returns {Promise} The JSON data or the rejected response
 */
function getJSON (response) {
  return response.ok ? response.json() : Promise.reject(response);
}

/**
 * Build the HTML string for a single story
 * @param {Object} story The story object
 * @returns {String} An HTML string
 */
function buildStory (story) {
  return `
    <article>
      <h2>
        <a href="${story.url}">
          ${story.title}
        </a>
      </h2>
      <p>
        <b>
          Last updated:
        </b>
        <time datetime="${story.updated_date}">
          ${new Date(story.updated_date).toLocaleString()}
        </time>
      </p>
      <p>
        ${story.abstract}
      </p>
    </article>
  `;
}

/**
 * Add the first five stories to the DOM
 * @param {Object} data The data returned by the API
 */
function showStories (data) {
  app.innerHTML = data.results.slice(0, 5).map(buildStory).join('');
}

/**
 * Add an error message to the DOM
 */
function showError () {
  app.innerHTML = `
    <p>
      <strong>
        Sorry, there was a problem fetching today's top stories. You can still view them using the link above.
      </strong>
    </p>
  `;
}

/**
 * Fetch today's stories and add them to the DOM
 */
function getStories () {
  fetch(endpoint)
    .then(getJSON)
    .then(showStories)
    .catch(showError);
}


//
// Inits & Event Listeners
//

// Fetch today's stories and add them to the DOM
getStories();
              
            
!
999px

Console