diff --git a/src/Text/Pandoc/Writers/Markdown.hs b/src/Text/Pandoc/Writers/Markdown.hs
index 0ac058893..2d9532dd3 100644
--- a/src/Text/Pandoc/Writers/Markdown.hs
+++ b/src/Text/Pandoc/Writers/Markdown.hs
@@ -470,13 +470,15 @@ blockToMarkdown' opts (Header level attr inlines) = do
           else return empty
   variant <- asks envVariant
   -- we calculate the id that would be used by auto_identifiers
+  -- or gfm_auto_identifiers
   -- so we know whether to print an explicit identifier
   ids <- gets stIds
   let autoId = uniqueIdent (writerExtensions opts) inlines ids
   modify $ \st -> st{ stIds = Set.insert autoId ids }
   let attr' = case attr of
                    ("",[],[]) -> empty
-                   (id',[],[]) | isEnabled Ext_auto_identifiers opts
+                   (id',[],[]) | (isEnabled Ext_auto_identifiers opts
+                                  || isEnabled Ext_gfm_auto_identifiers opts)
                                  && id' == autoId -> empty
                    (id',_,_)   | isEnabled Ext_mmd_header_identifiers opts ->
                                     space <> brackets (literal id')
diff --git a/test/command/7941.md b/test/command/7941.md
new file mode 100644
index 000000000..d68f04085
--- /dev/null
+++ b/test/command/7941.md
@@ -0,0 +1,6 @@
+```
+% pandoc -f commonmark_x -t commonmark_x
+# Class setup
+^D
+# Class setup
+```