@@ -13,7 +13,6 @@ pub(crate) const WARNING_TXT: &str = "warning";
1313/// Top-level user message
1414#[ derive( Clone , Debug ) ]
1515pub struct Message < ' a > {
16- pub ( crate ) id : Option < Id < ' a > > , // for "correctness", could be sloppy and be on Title
1716 pub ( crate ) groups : Vec < Group < ' a > > ,
1817}
1918
@@ -26,7 +25,15 @@ impl<'a> Message<'a> {
2625 ///
2726 /// </div>
2827 pub fn id ( mut self , id : & ' a str ) -> Self {
29- self . id . get_or_insert ( Id :: default ( ) ) . id = Some ( id) ;
28+ let Some ( Element :: Title ( title) ) =
29+ self . groups . get_mut ( 0 ) . and_then ( |g| g. elements . first_mut ( ) )
30+ else {
31+ panic ! (
32+ "Expected first element to be a Title, got: {:?}" ,
33+ self . groups
34+ ) ;
35+ } ;
36+ title. id . get_or_insert ( Id :: default ( ) ) . id = Some ( id) ;
3037 self
3138 }
3239
@@ -36,7 +43,15 @@ impl<'a> Message<'a> {
3643 ///
3744 /// </div>
3845 pub fn id_url ( mut self , url : & ' a str ) -> Self {
39- self . id . get_or_insert ( Id :: default ( ) ) . url = Some ( url) ;
46+ let Some ( Element :: Title ( title) ) =
47+ self . groups . get_mut ( 0 ) . and_then ( |g| g. elements . first_mut ( ) )
48+ else {
49+ panic ! (
50+ "Expected first element to be a Title, got: {:?}" ,
51+ self . groups
52+ ) ;
53+ } ;
54+ title. id . get_or_insert ( Id :: default ( ) ) . url = Some ( url) ;
4055 self
4156 }
4257
@@ -174,10 +189,41 @@ pub struct Padding;
174189#[ derive( Clone , Debug ) ]
175190pub struct Title < ' a > {
176191 pub ( crate ) level : Level < ' a > ,
192+ pub ( crate ) id : Option < Id < ' a > > ,
177193 pub ( crate ) title : & ' a str ,
178194 pub ( crate ) is_pre_styled : bool ,
179195}
180196
197+ impl < ' a > Title < ' a > {
198+ /// <div class="warning">
199+ ///
200+ /// This is only relevant if the title is the first element of a group.
201+ ///
202+ /// </div>
203+ /// <div class="warning">
204+ ///
205+ /// Text passed to this function is considered "untrusted input", as such
206+ /// all text is passed through a normalization function. Pre-styled text is
207+ /// not allowed to be passed to this function.
208+ ///
209+ /// </div>
210+ pub fn id ( mut self , id : & ' a str ) -> Self {
211+ self . id . get_or_insert ( Id :: default ( ) ) . id = Some ( id) ;
212+ self
213+ }
214+
215+ /// <div class="warning">
216+ ///
217+ /// This is only relevant if the title is the first element of a group and
218+ /// `id` present
219+ ///
220+ /// </div>
221+ pub fn id_url ( mut self , url : & ' a str ) -> Self {
222+ self . id . get_or_insert ( Id :: default ( ) ) . url = Some ( url) ;
223+ self
224+ }
225+ }
226+
181227/// A source view [`Element`] in a [`Group`]
182228///
183229/// If you do not have [source][Snippet::source] available, see instead [`Origin`]
0 commit comments