In a span of three days, Jerry Falwell Jr., a close ally of President Trump, has been in the national news twice — neither time for good reason. Over the weekend, Falwell said the president was the victim of a “failed coup” and deserves two more years tacked onto on his term. Then on Tuesday, Reuters reported that Trump fixer Michael Cohen helped Falwell get rid of some racy “personal” photos of the Liberty University president.

In the past two years, comments and actions by evangelical leaders close to the president have made it easy for many Americans to conclude that evangelicals are the foremost enemies of democracy and freedom.

As someone who writes about American religious history, I think this is truly unfortunate, because no one group has arguably done more — in the past — to advance religious freedom than evangelicals. Understanding the crucial evangelical role in crafting religious freedom makes the position of folks like Falwell all the more tragic, but it also hints at a better way forward for the evangelical movement as a whole.

Their biggest effect came through their influence on James Madison and other Framers who advocated most strenuously and cleverly for religious freedom.

Madison’s political coming of age coincided with a massive wave of persecution in his part of Virginia. Between 1760 and 1778, there were 150 major attacks against Virginia’s Baptists — whom we would now call evangelicals — many of them carried out by leaders of local churches associated with the Church of England, or Anglican parishes.

The Rev. John Waller was preaching in Caroline County, Va., in 1771 when an Anglican minister strode up to the pulpit and jammed the butt end of a horse whip into his mouth. Waller was dragged outside, where a local sheriff beat him bloody. He spent 113 days in jail — for the crime of being a Baptist preacher.

When the Rev. James Ireland was jailed in nearby Culpeper County, he continued to preach through his cell’s barred windows. To stop him, Anglican church leaders galloped horses through the crowd, and hecklers urinated in his face.

The Rev. David Thomas’s services were disrupted by protesters who hurled live snakes and a hornet’s nest into the room.

“This vexes me the most of any thing,” Madison, then 23, complained to his friend William Bradford in 1774. He told Bradford that five or six “well-meaning” Baptist ministers were at that moment imprisoned in neighboring Culpeper County for what he considered an absurd charge: preaching the gospel and “publishing their religious sentiments.”

“That diabolical, Hell-conceived principle of persecution rages,” he complained.

Through close exposure to the Baptist, or evangelical, cause, he came to understand that persecution was violent, but it also was bureaucratic. Baptists could be lashed by the law as well as the whip. The most common charges were preaching without a license or disturbing the peace. For instance, authorities imprisoned four preachers in Orange County, where Madison lived, for being “vagrant and Itinerant Persons and for Assembling themselves unlawfully at Sundry Times and Places Under the Denomination of Anabaptists and for Teaching & Preaching Schismatick Doctrines.”

Thomas Jefferson was also a key advocate of religious liberty — but from another perspective. Jefferson tended to emphasize the evils of priestly power and the need for freedom of thought. Madison, by contrast, believed that a separation of church and state would create a more vibrant religious foundation for the country. This idea — that even well-intended state support for religion would backfire — was influenced by the evangelicals, and was on display during Madison’s successful effort to block a plan to create state subsidies of salaries of Virginia’s ministers. Madison and the evangelicals believed such aid would corrupt faith by making it lazy and dependent on the state. The help violated Jesus’ injunction that his followers should render unto Caesar the things that are Caesar’s, and to God the things that are God’s, evangelicals argued.

The evangelical Baptists of Orange County declared that the idea that government should support religion was “founded neither in scripture, on Reason, on Sound Policy; but it is repugnant to each of them.”

Evangelicals were also an important voting bloc. In 1788, after the Constitution was ratified, Madison campaigned to serve in the first House. But his nemesis Patrick Henry, now Virginia’s governor, plotted to thwart Madison by creating an unreceptive congressional district and recruiting as his opponent James Monroe, a tall, dark and handsome future president who had crossed the Delaware with Washington and still carried bits of musket ball in his shoulder from the Battle of Trenton. Monroe’s allies had spread word that Madison had “ceased to be a friend to the rights of Conscience” and refused to make any changes to the Constitution. The Baptists were drifting away from Madison.

Because Madison needed their votes, he made perhaps the most consequential campaign promise in American history. He promised the evangelicals that, if elected, he would propose a Bill of Rights that safeguarded religious liberty. This satisfied them. Madison won his election, 1,308 to 972, as a result of lopsided majorities in the heavily Baptist areas of Culpeper and Orange counties. The evangelicals had delivered for Madison, and he fulfilled his campaign promise on June 8, 1789, by proposing a package of additions to the Constitution, including one that would become the First Amendment.

After the ratification of the Bill of Rights, evangelicals influenced the development of religious freedom in a different way. The First Amendment gave states tremendous freedom to define religious liberty for themselves, so the states spent the next few decades reshaping their own constitutions. In state after state, the old establishments — the systems of government support for particular religions — fell, often as a result of pressure from Baptists and other evangelicals who wanted to dispense with the regulation of faith.

About the same time, the country experienced what historians called the Second Great Awakening, an outpouring of evangelical fervor. From 1770 to 1860, the number of churches per capita grew by 1,470 percent, while the number of Christian ministers increased at three times the rate of the population. The churches that grew were disproportionately evangelical, staunch advocates for the dismantling of the religious regulations.

Another way evangelicals influenced religious freedom involved Rep. John Armor Bingham — now considered by scholars to be among the nation’s “second Founding Fathers.” He’s called that because he was the primary author of the 14th Amendment, passed right after the Civil War, which decrees that the Bill of Rights protects Americans from oppressive actions not only of the federal government but also of state and local lawmakers. It is the 14th Amendment that prevents, say, a local city council from unfairly denying a building permit to an unpopular synagogue, mosque or church.

Bingham grew up in a devout evangelical anti-slavery family, and he used highly religious language to describe the 14th Amendment’s origins. “It surged from my understanding of the Divine Plan for people,” he said. Bingham and his colleagues recognized the connection between religious liberty and other rights, including freedom of expression and the press. He discerned that because abolitionism was largely a religious movement, a statutory attack on the anti-slavery cause would often manifest as an attack on religion. For instance, the Rev. Jesse McBride, a Methodist preacher from Ohio, was arrested in South Carolina for giving a young white girl a pamphlet on the Ten Commandments that suggested slaveholders were violating God’s laws.

Bingham cast his effort in profoundly evangelical terms, explaining: “It surged from my understanding of the Divine Plan for people,” he said.

These days, I would argue, American evangelicals — white ones in particular — seem too often on the wrong side of history when it comes to religious freedom. True, their leaders speak a great deal about the topic. They fear that their biblically based opposition to same-sex marriage will prompt regulations that limit their ability to preach or live according to their faith. And there is some real validity to their concerns that secularism sometimes disadvantages religion in the public square.

But at the same time, evangelicals have participated in the greatest modern assault on religious freedom in the United States: attacks on Islam.

Franklin Graham — a leading Baptist minister — in 2002 called Islam a “wicked, violent religion.” Jerry Falwell, father of the one in the news this week, called Muhammad “a terrorist.” Jerry Vines, a former head of the Southern Baptist Convention, called Muhammad a “demon-possessed pedophile.” Most important, they have been fervent supporters of Trump, even after he took the historic step of proposing a ban on Muslim immigrants. While Jewish and Mormon groups opposed the plan, 61 percent of white evangelicals supported it.

It’s true that among the competing rights in the United States are those between the religiously orthodox and the more liberally secular. So at the exact moment they need the understanding of other religious groups, evangelicals have sacrificed much of their moral authority on this topic.

Prominent theologian Richard Mouw this week called for his fellow evangelicals to embrace “a generosity of spirit” when it comes to freedom and culture clashes. If we do, he argued, the further we’ll go “toward building a cultural consensus on the virtue of religious freedom for all.”

And preserving what is perhaps the nation’s greatest invention.

Steven Waldman is the author of “Sacred Liberty: America’s Long, Bloody and Ongoing Struggle for Religious Freedom.”