Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

11802 convert faq block titles to headings #12023

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added functionality to check for appropriate h tag
  • Loading branch information
Dieterrr committed Jan 11, 2019
commit 7fea80c936facd92eaf2c5006f1b26189ef9687b
32 changes: 29 additions & 3 deletions js/src/structured-data-blocks/faq/components/FAQ.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import React from "react";
import PropTypes from "prop-types";
import isUndefined from "lodash/isUndefined";
import { __ } from "@wordpress/i18n";
import forEach from "lodash/forEach";

/* Internal dependencies */
import Question from "./Question";
Expand Down Expand Up @@ -351,14 +352,38 @@ export default class FAQ extends Component {
);
}

/**
* Gets the correct header level for the questions in the FAQ block.
*
* @returns {string} The header tag.
*/
getHeader() {
const blocks = wp.data.select( "core/editor" ).getBlocks();
let header = "h2";

for ( let i = 0; i < blocks.length; i++ ) {
// Update header when preceding header is found.
if ( blocks[ i ].attributes.level ) {
header = "h" + ( blocks[ i ].attributes.level + 1 );
}
// Exit when faq-block is found.
if ( blocks[ i ].name === "yoast/faq-block" ) {
break;
}
}
return header;
}

/**
* Sets the header level for the questions in the FAQ block.
*
* @returns {void}
*/
setHeader(){
const blocks = wp.data.select( "core/editor" ).getBlocks();
this.props.setAttributes( { header: "h4" } );
setHeader() {
const newHeader = this.getHeader();
if ( this.props.attributes.header !== newHeader ) {
this.props.setAttributes( { header: newHeader } );
}
}

/**
Expand All @@ -368,6 +393,7 @@ export default class FAQ extends Component {
*/
render() {
this.setHeader();

const { className } = this.props;

const classNames = [ "schema-faq", className ].filter( ( i ) => i ).join( " " );
Expand Down